jQuery 不适用于 IE
我有以下 jQuery 脚本,它实际上被 Internet Explorer(7 和 8)忽略。在 FF 和 Chrome 中运行正常。
<script type="text/javascript" language="javascript">
$("body").addClass("newclass");
</script>
很简单,但不知道为什么IE会忽略它。知道代码是用 JAVA 作为动态内容加载的(这应该不是问题,因为其余脚本都可以工作)。我尝试将脚本作为外部文件中的函数调用,但也没有任何反应。谁能帮助我理解我的错误在哪里?或者帮助我了解IE?
I have the following jQuery script, which is actually being ignored by Internet Explorer (7 and 8). It works OK in FF and Chrome.
<script type="text/javascript" language="javascript">
$("body").addClass("newclass");
</script>
It's very simple, yet I don't know for what reason IE ignores it. Know that the code is loaded as dynamic content with JAVA (which shouldn't be a problem since the rest of the scripts work). I tried to call tha script as a function in a external file, but nothing happens either. Can anyone help me to understand where is my error? Or help me to understand IE?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者也许在文档的“就绪”事件中会更好
Or perhaps in the 'ready' event of the document would be better
我确信如果这是问题的话,您在 Firefox 和 Chrome 中也会遇到问题,但请尝试
确保在加载文档后调用它。也许 IE 需要这个。
I'm sure that you would have issues in Firefox and Chrome as well if this was the issue but try
just to be sure that it is called after the document is loaded. Maybe IE needs that.
将其包装在
$(document).ready(function(){ ... });
这样,JQuery 仅在页面完全加载后才运行它。
如果不这样做,代码将尽快执行,这可能是在 DOM 加载之前,因此您可能没有用于添加类的 body 元素。
一些浏览器可以工作而另一些浏览器则不能工作这一事实意味着不同的浏览器(a)以不同的速度加载页面,和/或(b)以不同的顺序执行初始加载任务。但您不必担心这一点。只需调用
$.ready()
,JQuery 就会按照正确的顺序完成这一切。Wrap it in a
$(document).ready(function(){ ... });
This way, JQuery will only run it once the page has full loaded.
If you don't do this, the code will be executed as soon as it can be, which may be before the DOM has loaded, so you may not have a body element to add the class to.
The fact that some browsers work and others don't implies that different browsers (a) load the page at different speeds, and/or (b) perform initial loading tasks in a different order. But you shouldn't need to worry about that. Just call
$.ready()
and it'll all be done in the right order for you by JQuery.