Javascript jQuery(document).ready() 不触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12542770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:40:11  来源:igfitidea点击:

jQuery(document).ready() does not fire

javascriptjquery

提问by Atadj

Does anyone know why jQuery document ready might not fire on a website? I put exactly this script in footer and it simply doesn't fire (jQuery 1.8 is included in head section):

有谁知道为什么 jQuery 文档就绪可能不会在网站上触发?我把这个脚本放在页脚中,它根本不触发(jQuery 1.8 包含在头部部分):

<script type="text/javascript">
jQuery(document).ready(function() { 
     alert('test');
     jQuery("#slideshow iframe").each(function(){ 
          alert('test');
     });
});
</script>

There are no Javascript errors, console is empty and when I run this in Firebug's console it works:

没有 Javascript 错误,控制台为空,当我在 Firebug 的控制台中运行它时,它可以工作:

jQuery("#slideshow iframe").each(function(){ 
     alert('test');
});

采纳答案by Nope

You are currently getting this error on your page

您目前在您的页面上收到此错误

Uncaught TypeError: Property '$' of object [object Window] is not a function 

The cause of this error is inside your flow.anything-slider-1.0.jsat line 11.

此错误的原因在您的flow.anything-slider-1.0.jsat 行中11

The file is using jQuery(document).ready(), so $is not defined.

该文件正在使用jQuery(document).ready(),因此$未定义。

Changing line 11from using $to jQueryworks:

将线路11从使用更改$jQuery工作:

// doesn't work
$("#content").before("<div id=\"cycledump\"></div>");

// Does work
jQuery("#content").before("<div id=\"cycledump\"></div>");

The whole file uses jQuery instead of $so the file should probably stick with the one way of using jQuery instead of mixing it up.

整个文件使用 jQuery 而不是$因此文件应该坚持使用 jQuery 而不是混合使用的一种方式。

Edit
I just double checked the .ready()documentation and the following paragraph was interesting as it seems to relate to the issue:

编辑
我只是仔细检查了.ready()文档,下面的段落很有趣,因为它似乎与问题有关:

Aliasing the jQuery Namespace
When using another JavaScript library, we may wish to call $.noConflict()to avoid namespace difficulties. When this function is called, the $shortcut is no longer available, forcing us to write jQueryeach time we would normally write $.

However, the handler passed to the .ready()method can take an argument, which is passed to the global jQuery object. This means we can rename the object within the context of our .ready()handler without affecting other code:

jQuery(document).ready(function($) {
  // Code using $ as usual goes here.
});

jQuery 命名空间的别名
当使用另一个 JavaScript 库时,我们可能希望调用$.noConflict()以避免命名空间困难。当这个函数被调用时,$快捷方式不再可用,迫使我们jQuery每次正常写的时候都要写$

但是,传递给该.ready()方法的处理程序可以接受一个参数,该参数将传递给全局 jQuery 对象。这意味着我们可以在.ready()处理程序的上下文中重命名对象,而不会影响其他代码:

jQuery(document).ready(function($) {
  // Code using $ as usual goes here.
});

This would imply, that instead of fixing line 11you could also change your fist line to jQuery(document).ready(function($) {, passing the $as an argument. This might allow you then to use $throughout the file as well as jQuery.

这意味着,除了固定线,11您还可以将拳头线更改为jQuery(document).ready(function($) {,将 传递$为参数。这可能允许您$在整个文件以及 jQuery 中使用。

Anyway, not sure passing $as an argument would work in your case, I just thought I mention it in case it does work.

无论如何,不​​确定$作为参数传递是否适用于您的情况,我只是想我会提到它以防万一。

回答by Tgr

While that was not the case in this specific instance, one very annoying way in which this can happen is jQuery bug 10251: an error in a $(document).ready()callback will prevent all callbacks registered after it from running. (In this case there will be a Javascript error, it might look completely unrelated, though.)

虽然在这个特定实例中情况并非如此,但一种非常烦人的方式是jQuery 错误 10251$(document).ready()回调中的错误将阻止在它之后注册的所有回调运行。(在这种情况下,会出现 Javascript 错误,不过它可能看起来完全无关。)

回答by Dipak

It can be jQuery library path issue. Try loading it from jquery.com-

它可能是 jQuery 库路径问题。尝试从jquery.com加载它-

<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>

<script type="text/javascript">

$(document).ready(function() { 
     alert('test');
});

</script>

Copy this code and paste in headsection

复制此代码并粘贴到head部分

Edit:

编辑:

You are using jQuery 1.4.2and 1.8 jQuery UI

您正在使用jQuery 1.4.21.8 jQuery UI

Try moving the script tagin headsection, it should work

尝试移动script taginhead部分,它应该可以工作