jQuery 不适用于 IE

发布于 2024-10-10 14:41:04 字数 336 浏览 7 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕梦 2024-10-17 14:41:04

或者也许在文档的“就绪”事件中会更好

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

Or perhaps in the 'ready' event of the document would be better

$(document).ready(function() {
  $('body').addClass('newclass');
});
冰之心 2024-10-17 14:41:04

我确信如果这是问题的话,您在 Firefox 和 Chrome 中也会遇到问题,但请尝试

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

确保在加载文档后调用它。也许 IE 需要这个。

I'm sure that you would have issues in Firefox and Chrome as well if this was the issue but try

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

just to be sure that it is called after the document is loaded. Maybe IE needs that.

无人接听 2024-10-17 14:41:04

将其包装在 $(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文