jquery DOM 操作在 IE8 中非常慢,尤其是 addClass 和 removeClass

发布于 2024-10-03 09:21:14 字数 638 浏览 0 评论 0原文

我面临着一个非常奇怪的问题。我的 html 中有选项卡和子选项卡,当我单击选项卡/子选项卡时,会在其上放置“activeContent”类。如果我单击另一个选项卡/子选项卡,“activeContent”类将从前一个选项卡/子选项卡中删除并放置在当前选项卡/子选项卡上。当我不断单击多个选项卡/子选项卡时,这种情况效果很好。但在 IE8 中速度非常慢。特别是当我点击后退按钮时,会加载前一个子选项卡的内容,但活动子选项卡需要花费大量时间来更改其类。其效果是,虽然其他选项卡的内容相同,但活动子选项卡仍然是上一个选项卡。

我什至尝试首先更改选项卡/子选项卡类,例如

$(currentTab.node).removeClass('activeContent');
$(tab.node).addClass('activeContent');

然后使用 seTimeout ,例如在执行上述代码之后。

setTimeout(fuunction(){

//load ajax content
}, 800);

即使如此,选项卡/子选项卡也需要花费大量时间来更改其类别。

这是 IE8 还是我可能必须优化我的代码。我不知道。在包括 IE6 在内的所有其他浏览器中一切正常。和IE8的后退按钮有关系吗?

I am facing a very strange issue. I have tabs and subtabs in my html and when i click on a tab/subtab 'activeContent' class is placed on it. if i click on another tab/subtab the 'activeContent' class is removed from the previous tab/subtab and placed on the current one. While this scenario works fine when i keep clicking on multiple tabs/subtabs. But in IE8 its very slow. Especially when i hit the back button, the content from the previous subtab is loaded but the active subtab takes a lot of time to change its class. The effect of it is that while that while the content if of some other tab while the active subtab is still the previuos one.

I have even tried to first change the tab/subtab class, something like

$(currentTab.node).removeClass('activeContent');
$(tab.node).addClass('activeContent');

and then used a seTimeout , something like after the above code gets executed.

setTimeout(fuunction(){

//load ajax content
}, 800);

Even then the tabs/subtabs takes a lot of time to change its class.

Is this a IE8 or i might i have to optimize my code. I am not sure. Everything works fine in all other browsers including IE6. Is it has something to do with the back button in IE8?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

油焖大侠 2024-10-10 09:21:14

当您点击后退按钮时,您是否会调用此代码?后退按钮很可能会导致页面刷新,而您正在等待整个页面重新加载。 IE8 可能只是让这种行为更加明显,因为它对页面内容的缓存处理方式略有不同。

Are you calling this code when you hit the back button? Most likely the back button is causing a page refresh, and you are waiting for the whole page to reload. IE8 is probably just making this behavior more obvious, because it is handing the caching of page content a little differently.

[旋木] 2024-10-10 09:21:14

我有一个替代解决方案给你。这是锚标记上的点击事件吗?我注意到 IE 需要花费大量时间来取消对具有 href 属性的锚标记的默认操作。尤其是在IE8中。

下面是我网站上的一个示例函数:

function SwapLinks() { // This allows our pages to degrade gracefully. But hrefs are slow. So, if JS is enabled remove the href!
$(".playerLink").each(function (index) {
    var link = $(this).attr("href");
    if (link != undefined && link != null && link != "") {
        $(this).removeAttr("href");
        $(this).attr("link", ""); // This little number makes IE6/IE7 happy.
        $(this).attr("link", link);

    }
});

然后您将在 (“.playerLink”) 上添加一个处理 Ajax 更新的单击事件。

I have an alternative solution for you. Is this a click event on an anchor tag? I have noticed that it takes an exorbitant amount of time for IE to cancel the default action on an anchor tag that has a href property. Especially in IE8.

Here is an example function from my site:

function SwapLinks() { // This allows our pages to degrade gracefully. But hrefs are slow. So, if JS is enabled remove the href!
$(".playerLink").each(function (index) {
    var link = $(this).attr("href");
    if (link != undefined && link != null && link != "") {
        $(this).removeAttr("href");
        $(this).attr("link", ""); // This little number makes IE6/IE7 happy.
        $(this).attr("link", link);

    }
});

Then you would add a click event on (".playerLink") that handles the Ajax updating.

望喜 2024-10-10 09:21:14

其实我的代码没有问题。我在朋友的机器上测试过,运行良好。然后我重置了 IE8,一切开始正常工作。我不确定为什么 IE8 会这样。它也发生在早些时候,我不得不重置 IE8,因为它无法识别在我的本地计算机上的 jboss 服务器上运行的应用程序 http://my-pc:8080/myapp/mypage.html 但我必须做 http://167.232.23.12/myapp/mypage.html 然后它会显示所有内容。所以当我重置浏览器时,我可以通过运行我的应用程序
http://my-pc:8080/

There was no problem with my code actually. I tested on a friends machine and it was working fine. Then i reset IE8 and everything started to work fine. I am not sure why IE8 was behaving in that way. It happened earlier also, I had to reset IE8 because it was not recognizing the app running on jboss server on my local machine by doing this http://my-pc:8080/myapp/mypage.html BUT rather i had to do http://167.232.23.12/myapp/mypage.html and then it would display evrything. So when i reset the browser , i could run my app through
http://my-pc:8080/ .

冷月断魂刀 2024-10-10 09:21:14

我也遇到了这个问题,结果发现这是因为我忘记从 click() 事件中返回 return false; 。 (我想 e.preventDefault() 也会起作用。)

从那时起,我就一直在我的选项卡中使用像 这样的链接它并没有真正导航到任何地方,但 IE 似乎正在“尝试”导航并花费时间来执行此操作,因此返回 false 会阻止真正的导航。 (这可能是最佳实践,让我放入“真实”链接来回退,这可能也是最佳实践。)

当我使用 file:/ 加载页面时,这似乎尤其是一个问题/ 我的开发计算机上的 URL(而不是将其部署到服务器并通过 HTTP 以常规方式访问它)。

(感谢 Jeff Davis 和 kd44,他们的上述回答让我走上了正轨。)

I had this problem too, and it turned out it was because I was forgetting to return false; from the click() event. (I imagine e.preventDefault() would work, too.)

I'd been using a link like <a href="#"> for my tabs since it doesn't really navigate anywhere, but IE seem to be "trying" to navigate and taking time to do so, so returning false prevents the navigation for real. (And is probably a best practice, and let's me put in "real" links to fall back to which is probably also a best practice.)

It seems especially a problem when I've loaded the page with a file:// URL on my development machine (as opposed to deploying it to a server and accessing it in the regular way via HTTP).

(Thanks to Jeff Davis and kd44 whose answers above put me on the right track.)

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