简体   繁体   English

jQuery不适用于IE

[英]jQuery doesn't work on IE

I have the following jQuery script, which is actually being ignored by Internet Explorer (7 and 8). 我有以下jQuery脚本,实际上被Internet Explorer(7和8)忽略。 It works OK in FF and Chrome. 它在FF和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. 这很简单,但我不知道IE为什么会忽略它。 Know that the code is loaded as dynamic content with JAVA (which shouldn't be a problem since the rest of the scripts work). 知道代码是作为JAVA的动态内容加载的(这应该不是问题,因为其余的脚本工作)。 I tried to call tha script as a function in a external file, but nothing happens either. 我试图将tha脚本作为外部文件中的函数调用,但也没有任何反应。 Can anyone help me to understand where is my error? 任何人都可以帮我理解我的错误在哪里? Or help me to understand IE? 还是帮我理解IE?

Or perhaps in the 'ready' event of the document would be better 或者也许在文档的“准备好”事件中会更好

$(document).ready(function() {
  $('body').addClass('newclass');
});

I'm sure that you would have issues in Firefox and Chrome as well if this was the issue but try 我确定你会在Firefox和Chrome中遇到问题,如果这是问题,但试试吧

$(function () {
    $('body').addClass('newclass');
});

just to be sure that it is called after the document is loaded. 只是为了确保在加载文档后调用它。 Maybe IE needs that. 也许IE需要这个。

Wrap it in a $(document).ready(function(){ ... }); 将它包装在$(document).ready(function(){ ... });

This way, JQuery will only run it once the page has full loaded. 这样,JQuery只会在页面完全加载后运行它。

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. 如果你不这样做,代码将尽快执行,这可能是在DOM加载之前,所以你可能没有一个body元素来添加类。

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. 某些浏览器工作的事实和其他浏览器并不意味着不同的浏览器(a)以不同的速度加载页面,和/或(b)以不同的顺序执行初始加载任务。 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. 只需调用$.ready() ,它就可以通过JQuery以正确的顺序完成。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM