简体   繁体   English

包含 .js 文件阻止 jQuery 加载

[英]included .js file preventing jQuery from loading

I am new to jquery/web design and download a template to try and familiarize myself.我是 jquery/web 设计的新手,并下载了一个模板来尝试熟悉自己。 I am having an issue with a certain script that is preventing my jQuery from loading.我遇到了某个脚本的问题,该脚本阻止了我的 jQuery 加载。 I have this in the header on my site:我在我网站的标题中有这个:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>
<script type="text/javascript" src="layout/scripts/jquery-mobilemenu.min.js"></script>
<script type="text/javascript" src="layout/scripts/responsiveslides.js-v1.53/responsiveslides.min.js"></script>
<script src="layout/scripts/custom.js"></script>

The culprit file is the <script type="text/javascript" src="layout/scripts/jquery-mobilemenu.min.js"></script> ;罪魁祸首是<script type="text/javascript" src="layout/scripts/jquery-mobilemenu.min.js"></script> everytime I disable this line jQuery works fine.每次我禁用这一行 jQuery 工作正常。 I am really not sure what to identify in the file that would cause jQuery to not load.我真的不确定要在文件中识别哪些会导致 jQuery 无法加载的内容。 The contents of js: js的内容:

jQuery.noConflict()(function ($) {
    // Create the dropdown base
    $("<form id='mobilemenu'><select /></form>").appendTo("#topnav");
    // Create default option "Go to..." 
    $("<option />", {
        "selected": "selected",
        "value": "",
        "text": "Click For Menu"
    }).appendTo("#topnav select");
    //Populate dropdown with menu items
    $("#topnav a").each(function () {
        var el = $(this);
        var prefix = '';
        switch (el.parents().length) {
            case (6):
                prefix = '';
                break;
            case (8):
                prefix = '- - - ';
                break;
            case (10):
                prefix = '- - - - - ';
                break;
            case (12):
                prefix = '- - - - - - - ';
                break;
            default:
                prefix = '';
                break;
        }
        $("<option />", {
            "value": el.attr("href"),
            "text": prefix + el.text()
        }).appendTo("#topnav select");
        $("#topnav select").change(function () {
            window.location = $(this).find("option:selected").val();
        });
    });
});

Would anyone be able to point me in the right direction with correcting this file?任何人都可以通过更正此文件为我指出正确的方向吗? I have tried to move the order that my scripts load and it doesn't matter the order, jQuery doesn't load when the mobilemenu script is included.我试图移动我的脚本加载的顺序,但顺序无关紧要,当包含 mobilemenu 脚本时 jQuery 不会加载。

Within the script you mention as causing the problem you will see a call to noConflict() this method allows jQuery to be used with other libraries that also rely on the $ as an entry point by removing $ as an alias to jQuery.在您提到导致问题的脚本中,您将看到对noConflict()的调用,此方法允许 jQuery 与其他依赖$作为入口点的库一起使用,方法是删除$作为 jQuery 的别名。

Immediately following the noConflict() call you see a very specific function signature that is being passed to jQuery:noConflict()调用之后,您会立即看到传递给 jQuery 的非常具体的函数签名:

(function ($) {
  //function code here
});

jQuery is going to call this function and when it does it will pass itself in as the $ parameter and within the scope of that function you now access jQuery using $ again. jQuery 将调用此函数,当它调用时,它会将自身作为$参数传入,并且在该函数的范围内,您现在再次使用$访问 jQuery。

So with the script the way it is you will need to use jQuery by name.因此,对于脚本,您将需要按名称使用 jQuery。

Have a look at the documentation on noConflict for more details.有关更多详细信息,请查看有关noConflict文档

There are also several other questions on SO with regards to noConflict , this one might help in particular.关于noConflict SO 还有其他几个问题, 这个问题可能特别 帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM