jquery(document).ready() 无法在 IE 7 中运行
这对我来说似乎是一个非常简单的问题,但尽管谷歌搜索了所有可能的搜索字符串,我还是找不到答案。
我正在使用 jquery 1.5.1 并尝试使用 $(document).ready() 函数在页面加载时绑定多个单击事件。最终我将问题缩小到这么小:
$(document).ready(function(){
alert('hello world');
});
在 IE 6,7 和 8 中,当我加载页面时,我从未看到 hello world 警报。 IE 9 可以工作,FF、Chrome、Safari 等也可以。
一种有效的解决方案是将警报置于 500 毫秒的 JS 超时中。这似乎只是有时有效,但绝对不够一致。
我能想到的唯一可能性是,因为这个脚本恰好加载在页面 HTML 的 body 标记内,所以这可能会弄乱 read() 函数。如果是这样的话,我不知道该怎么解决。
更新
好的,在测试较小的页面后,我可以让文档准备好在 IE 中工作(尽管没有警报,我想这是一个完全不同的问题)。
就可以在页面中加载 javascript 文件的位置而言,IE 有什么怪癖吗?
This seems like really simple problem to me, but despite googling every possible search string, I cannot find an answer.
I'm using jquery 1.5.1 and trying to use the $(document).ready() function to bind several click events when the page loads. Eventually I narrowed down the problem to be this small:
$(document).ready(function(){
alert('hello world');
});
In IE 6,7, and 8 I never see the hello world alert when I load the page. IE 9 works though, as does FF, Chrome, Safari etc.
A solution that sort of worked was to surround the alert in a 500ms JS timeout. This only seems to work sometimes but definitely not consistently enough.
The only possibility I can think of is that because this script happens to be loaded well inside the body tag of the page HTML, this might be messing up the ready() function. If that's the case, I don't know what the solution would be.
UPDATE
OK, after testing a smaller page I can get the document ready to work in IE (no alerts though, I guess that's a completely different problem).
Are there any quirks regarding IE in terms of where you can load the javascript files in the page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
type=""
属性中有什么内容? IE 对于该属性非常严格。如果您指定该属性,则它必须包含"text/javascript"
否则 IE 将忽略它。What do you have in the
type=""
attribute? IE is very strict when it comes to that attribute. If you specify that attribute, it must contain"text/javascript"
or else IE will ignore it.没有理由在页面正文中使用
$(document).ready
。当您的 SCRIPT 标记位于页面的 HEAD 中时使用它,否则将其删除,并且在所有必要的正文元素关闭之前不要添加 SCRIPT 标记。There's no reason to use
$(document).ready
inside the BODY of the page. Use it when your SCRIPT tag is in the HEAD of the page, or else remove it and don't add your SCRIPT tag until after all the necessary body elements are closed.您是否有任何其他可能与 jQuery $ 冲突的 javascript 文件?
页面加载时进行测试,
Do you have any other javascript files that could be conflicting with the jQuery $?
Conduct a test when the page loads,
因此,在搞乱之后,我得出的结论是我在虚拟机中使用的 IE 浏览器有问题。无论我对 javascript 进行什么更改,按钮和 ajax 调用都无法正常工作,这似乎是随机且间歇性的。我认为准备好的文档一直在工作,浏览器以不同的方式失败。现在似乎正在发挥作用。
So after messing with this I'm arriving at the conclusion that the IE browsers I'm using in a VM are at fault. The failure of the buttons and ajax calls to work correctly seems to be random and intermittent regardless of changes I make to the javascript. I think the document ready had been working all along and the browsers were failing in different ways. It seems to be working now.
我在使用 IE7 时遇到了这个问题,似乎我正在使用
将第一个脚本标记更改为
“似乎”来使其工作。希望这有帮助。
I've had this problem with IE7, it seems that I was using
Changing the first script tag to
Seems to make it work. Hope this helps.