IE JQuery 就绪功能不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8855985/
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
IE JQuery ready function not working
提问by Elie
Apparently many people have run into this problem, but I have yet to be able to find a solution that works.
显然很多人都遇到过这个问题,但我还没有找到有效的解决方案。
I have a bit of code which needs to run once the page has loaded, and so I stuck it inside the following block:
我有一些代码需要在页面加载后运行,所以我把它放在以下块中:
$(document).ready(function() {
alert("Running initialization");
initialize();
});
function checkDivLoaded() {
if ( $('#footer').length == 0) $.error( 'not ready' );
}
function initialize() {
try{
checkDivLoaded();
...more code here
} catch (err) {
setTimeout(initialize, 200);
}
}
This works fine in all browsers, with the exception of IE. There, the code does not execute at all.
这适用于所有浏览器,IE 除外。在那里,代码根本不执行。
This code is at the lowest point on the page that I can put it (using the Zend Framework and page-specific ready() functions means that there is a limit to how low on the page it can go). I've checked the includes for the js files, which are all being loaded from a local version, and they all have the form
这段代码位于我可以放置的页面上的最低点(使用 Zend 框架和特定于页面的 ready() 函数意味着它可以在页面上到达多低的位置是有限制的)。我检查了 js 文件的包含,这些文件都是从本地版本加载的,它们都有这样的形式
<script type="text/javascript" src=""></script>
Any ideas?
有任何想法吗?
NOTE
笔记
When I open the debugger in IE, this starts to work correctly.
当我在 IE 中打开调试器时,它开始正常工作。
回答by Rocket Hazmat
I don't see anything wrong here, but there are just a few things I'd like to point out.
我看不出这里有什么不妥,但我想指出几点。
Before I do that, check IE's JavaScript console, my guess is an earlier script has an error, and that's why this one never gets run.
在我这样做之前,请检查 IE 的 JavaScript 控制台,我的猜测是早期的脚本有错误,这就是为什么这个脚本永远不会运行的原因。
Instead of $(document).ready(function(){});
, you can actually just do $(function(){})
. it shouldn't make a difference, though.
取而代之的是$(document).ready(function(){});
,您实际上可以这样做$(function(){})
。不过应该没什么区别。
Also, please don't pass strings to setTimeout
(they get eval
'd in the global scope), pass a function.
另外,请不要将字符串传递给setTimeout
(它们eval
在全局范围内被传递),而是传递一个函数。
setTimeout(initialize, 200);
Also, are you sure you declared $jquery
(shouldn't it be $.error
)?
另外,你确定你声明了$jquery
(不应该是$.error
)?
<script type="text/javascript">
$(function() {
initialize();
});
function checkDivLoaded() {
if ($('#footer').length == 0){
$jquery.error( 'not ready' );
}
}
function initialize() {
try{
checkDivLoaded();
//...more code here
} catch (err) {
setTimeout(initialize, 200);
}
}
</script>
EDIT: If you're using IE8 or lower, it doesn't add console
unless the debugger is open (I wish whoever thought that was a good idea has been fired). If one of your includes uses console.log
it would be undefined
, thus stopping all script execution.
编辑:如果您使用的是 IE8 或更低版本,console
除非调试器打开,否则它不会添加(我希望认为这是个好主意的人已被解雇)。如果您的一个包含使用console.log
它会是undefined
,从而停止所有脚本执行。
Try adding this to the top of the page:
尝试将其添加到页面顶部:
window.console = window.console || {log:function(){}};
It will make a "fake" console
object, so that console.log
will be defined.
它将制作一个“假”console
对象,因此console.log
将被定义。
回答by astax
Just had the same problem. Nearly broken my brain trying to figure out what's wrong.
刚刚有同样的问题。试图弄清楚出了什么问题,我的大脑几乎崩溃了。
Turned to be very simple - some code that's executed earlier used console.log(...), but it isn't defined in IE unless the dev tools are enabled.
变得非常简单 - 之前执行的一些代码使用了console.log(...),但除非启用了开发工具,否则它不会在 IE 中定义。
The fix is simple - either remove/comment console.log or add this code to one of your files before the first console.logis used:
修复很简单 - 在使用第一个console.log之前删除/评论 console.log 或将此代码添加到您的一个文件中:
if (typeof console === "undefined"){
console={};
console.log = function(){};
}
回答by Ritesh Kumar Sahu
Write below two lines at top of the page.
在页面顶部写下两行。
window.console = window.console || {log:function(){}};
if (typeof console === "undefined"){
console={};
console.log = function(){};
}
This solved my problem
这解决了我的问题
回答by ROROROOROROR
don't know where I've found this solution, but this does work for my IE10.
不知道我在哪里找到了这个解决方案,但这确实适用于我的 IE10。
An example from my project:
我的项目中的一个例子:
var windowOnload = window.onload || function() {
$('.toInitiate')[0].click();
$('a').click(function() {
if(this.className.indexOf("collapse") == -1)
{
$('.active').removeClass('active');
$(this).parent().addClass('active');
var className = $(this).parent().attr('class');
}
});
};
window.onload = function() {
windowOnload();
};
These codes are executed after loading all the html contents.
这些代码在加载所有 html 内容后执行。
回答by Glenn
This worked for me. I Replaced
这对我有用。我替换了
$(document).ready(function...
with
和
jQuery(document).ready(function...
回答by ExillustX
Using (function($) {})(jQuery);
fixed the issue for me.
使用(function($) {})(jQuery);
为我解决了这个问题。
(function($) {
// body code
})(jQuery);
回答by Dylan Bijnagte
Try putting your
试着把你的
$(document).ready(function...
at the bottom of the code. It could be that IE has not parsed the initialize()
code prior to the ready function being called.
在代码的底部。可能是 IEinitialize()
在调用就绪函数之前没有解析代码。