0

我有一个网页,我在标题中加载了一些 javascript 文件。一些子页面将加载额外的 javascript 文件。在主页上一切正常,但在子页面上我得到了很多这样的异常:

未捕获的类型错误:对象 [object Object] 的属性“$”不是函数

我可以看到这个异常发生在 details.js、voteHandler.js 和 HTML 页面本身中的 4 次。总是在这一行抛出异常:

$("document").ready(function () {

这是工作的主页的样子:

<head>
        <script type="text/javascript" src=/Scripts/jquery-1.7.1.min.js></script>
        <script type="text/javascript">
            //URL for voting
            var _postVoteUrl = 'http://localhost:5215/Post/Vote'
            //URL for tags
            var _tagsUrl = 'http://localhost:5215/Post/Tags'

            //Keep track of if a cascading is loading, if so, cancel submits
            var cascadingControlLoading = false;
            window.latestClick = '';

            function IsNotDblClick(objectID) {
                if (window.latestClick != objectID &&
                    !cascadingControlLoading) {
                    window.latestClick = objectID;
                    return true;
                } else {
                    return false;
                }
            }

            $(document).ready(function () {
                if($('#rightCon').text().trim().length < 1)
                {$('#rightCon').hide();}
            });

        </script>
        <script type="text/javascript" src=/Scripts/jquery-ui-1.8.20.min.js>"></script>
        <script type="text/javascript" src=/Scripts/jquery.elastic.source.js></script>
        <script type="text/javascript" src=/Scripts/jquery.validate.min.js></script>
        <script type="text/javascript" src=/Scripts/jquery.validate.unobtrusive.min.js></script>
        <script type="text/javascript" src=/Scripts/jquery.qtip.min.js></script>  
        <script type="text/javascript" src=/Scripts/formhandler.js></script>
        <script type="text/javascript" src=/Scripts/taghandler.js></script>
        <script src="/Scripts/voteHandler.js"></script>
        <script type="text/javascript" src=/Scripts/select2.min.js %>"></script>

    <script>
        function TogglePostCon() {
            $('#postListEditorCon').toggle();
        }


        SetupTagTextBox("txtTagBox", false);
        SetupTagTextBoxPersonalTag("txtPersonalTagBox", true);
        SetupTagTextBoxPersonalTag("txtPersonalIgnoreTagBox", true);
    </script>

        <script src="/Scripts/modernizr-2.5.3.js"></script>
    </head>

这是引发异常的子页面:

    <head>
            <script type="text/javascript" src=/Scripts/jquery-1.7.1.min.js></script>
            <script type="text/javascript">
                //URL for voting
                var _postVoteUrl = 'http://localhost:5215/Post/Vote'
                //URL for tags
                var _tagsUrl = 'http://localhost:5215/Post/Tags'

                //Keep track of if a cascading is loading, if so, cancel submits
                var cascadingControlLoading = false;
                window.latestClick = '';

                function IsNotDblClick(objectID) {
                    if (window.latestClick != objectID &&
                        !cascadingControlLoading) {
                        window.latestClick = objectID;
                        return true;
                    } else {
                        return false;
                    }
                }

                $(document).ready(function () {
                    if($('#rightCon').text().trim().length < 1)
                    {$('#rightCon').hide();}
                });
            </script>
            <script type="text/javascript" src=/Scripts/jquery-ui-1.8.20.min.js>"></script>
            <script type="text/javascript" src=/Scripts/jquery.elastic.source.js></script>
            <script type="text/javascript" src=/Scripts/jquery.validate.min.js></script>
            <script type="text/javascript" src=/Scripts/jquery.validate.unobtrusive.min.js></script>
            <script type="text/javascript" src=/Scripts/jquery.qtip.min.js></script>  
            <script type="text/javascript" src=/Scripts/formhandler.js></script>
            <script type="text/javascript" src=/Scripts/taghandler.js></script>
            <script src="/Scripts/details.js"></script>
            <script src="/Scripts/voteHandler.js"></script>
       <script>
$(function () {

                //Google +1
                $.getScript("http://apis.google.com/js/plusone.js", null, true);

                //Twitter
                $.getScript("http://platform.twitter.com/widgets.js", null, true);

                //Facebook
                $.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1", function () {

                    $('body').append('<div id="fb-root"></div>');

                    FB.init({ status: true, cookie: true, xfbml: true });

                }, true);
            });
    </script>

        <script src="/Scripts/modernizr-2.5.3.js"></script>
    </head>

我之前在正文底部加载了一些脚本,这并没有产生异常,但从我读到的内容来看,这不是推荐的方法。

那么为什么我的子页面会产生这些异常呢?

4

1 回答 1

0

在“无冲突”模式下,$快捷方式不可用,使用较长的jQuery,即

jQuery(document).ready(function ($) {

通过在函数调用后的括号中包含 $,您可以在代码块中使用此快捷方式。

替换你的代码

$(document).ready(function () {
                    if($('#rightCon').text().trim().length < 1)
                    {$('#rightCon').hide();}
                });

有了这个

jQuery(document).ready(function ($) {
                    if($('#rightCon').text().trim().length < 1)
                    {$('#rightCon').hide();}
                });
于 2013-09-21T08:21:18.517 回答