简体   繁体   English

$(document).ready(function(){…})在插件脚本加载之前运行

[英]$(document).ready(function() {…}) runs before plugin script loads

I'm getting a JS error because my $(function () {...}) handler is being fired seemingly before the prerequisite plugin script is loaded. 我收到JS错误,因为在加载必备插件脚本之前,似乎触发了我的$(function () {...})处理函数。 Only happens in IE (testing in IE7). 仅在IE中发生(在IE7中进行测试)。

I have some HTML in my <head> that looks like this: 我的<head>中有一些HTML,如下所示:

<script type="text/javascript" src="../resources/org.wicketstuff.jwicket.JQuery/jquery-1.4.2-special.js"></script>
...
<script type="text/javascript" id="noConflict"><!--/*--><![CDATA[/*><!--*/
jQuery.noConflict();
/*-->]]>*/</script>
...
<script type="text/javascript" src="../resources/com.csc.aims.wicket.components.collapsiblefieldset.CollapsibleFieldsetBehavior/jquery.collapsiblefieldset.js"></script>
<link rel="stylesheet" type="text/css" href="../resources/com.csc.aims.wicket.components.collapsiblefieldset.CollapsibleFieldsetBehavior/jquery.collapsiblefieldset.css" />
<script type="text/javascript">
jQuery(function(){
jQuery('#collapse119').collapse({"iconClosedUrl":"../resources/img/white_plus","iconOpenUrl":"../resources/img/white_minus"});
});
</script>

So notice the sequence, according to the HTML code, is as follows: 因此请注意,根据HTML代码的顺序如下:

  1. jquery-1.4.2-special.js jQuery的1.4.2 special.js
  2. jQuery.noConflict() call jQuery.noConflict()调用
  3. jquery.collapsiblefieldset.js //defines $.fn.collapse jquery.collapsiblefieldset.js //定义$ .fn.collapse
  4. jQuery('#collapse119').collapse(...) is called jQuery('#collapse119')。collapse(...)被称为

When this code runs in FF, everything works fine. 当此代码在FF中运行时,一切正常。 When I test it in IE7 (or IE8 w/Compat. View: IE7 standards mode), I get a javascript error. 当我在IE7(或带Compat。View的IE8:IE7标准模式)下进行测试时,出现JavaScript错误。 The debugger shows me that jQuery.fn.collapse is undefined. 调试器向我显示jQuery.fn.collapse是未定义的。

Using the IE8 developer tools, I try to look at jquery.collapsiblefieldset.js. 使用IE8开发人员工具,我尝试查看jquery.collapsiblefieldset.js。 I see the script in the list, but the tool tells me that I can't set a breakpoint because the script isn't loaded. 我在列表中看到了脚本,但是该工具告诉我,因为未加载脚本,所以无法设置断点。

Why does the collapsiblefieldset.js not get loaded before my $() ready handler is run? 为什么在我的$()ready处理程序运行之前,collapsiblefieldset.js没有加载? Any insight would be appreciated! 任何见识将不胜感激! Thanks. 谢谢。

You're using 您正在使用

$(function(){...});

which is synonym of 这是的同义词

$(document).ready( function(){...} );

Instead, you might try 相反,您可以尝试

$(window).load( function(){...} );

which fires later in the page loading sequence. 在页面加载顺序的稍后触发。

Put all of your scripts at the bottom of the page, right before the </body> tag. 将所有脚本放在页面底部</body>标记之前。

If that doesn't fix it, move the scripts that do not appear to be loading in time back to the <head> , and leave the rest of the scripts at the bottom. 如果仍不能解决问题,请将未及时加载的脚本移回<head> ,并将其余脚本保留在底部。

To anyone else suffering from this problem, it might be worth double checking that you don't have multiple references to jQuery. 对于其他遭受此问题困扰的人,值得仔细检查一下您没有对jQuery的多个引用。 If you have plugins defined in the head, then they will get overridden by a second jQuery call in the body. 如果您在头中定义了插件,则它们将被正文中的第二个jQuery调用覆盖。

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

相关问题 命名已准备好在文档上加载的功能 - Naming a function that loads on document ready jQuery(document).ready仍在文档准备好之前运行吗? - JQuery (document).ready still runs before the document is ready? 淘汰嵌套组件:$(document).ready()…在加载嵌套组件之前运行 - Knockout nested components: $(document).ready() … runs before nest component is loaded 在加载并运行脚本之前,在<head>中编辑<script>标记 - Edit a <script> tag in the <head> before it loads and runs the script 在$(document).ready()之前运行一个函数 - Running a function just before $(document).ready() triggers $(document).Ready()加载Flexigrid,但是在$(document).Ready()之外的其他函数再次调用时不会 - $(document).Ready() loads Flexigrid, but not when called again by other function outside $(document).Ready() 如何转换 script.js 文档就绪函数包装器和 jquery 插件方法以在 React(16.12.0) 中使用 - How to convert a script.js document ready function wrapper and jquery plugin methods for use in React(16.12.0) 在文档加载到JS之前调用函数 - Calling a function before the document loads up in JS 为什么 '$(document).ready(function()' 不适用于此脚本? - Why '$(document).ready(function()' is not working for this script? $(文件)。就绪(函数(){}); vs页面底部的脚本 - $(document).ready(function(){}); vs script at the bottom of page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM