gpt4 book ai didi

在 IE 中使用 'not' 的 JQuery 性能问题

转载 作者:行者123 更新时间:2023-12-01 01:37:47 24 4
gpt4 key购买 nike

当我使用 jquery every 且不在 every 内部调用时,会对 IE 浏览器产生性能影响,并出现错误“停止运行脚本”。

使用 $("li:not(.sel)", this).hide(); 语句出现性能问题。

尝试过 $("li", this).not(".sel").hide();$("li[class != sel]", this ).hide();

当我使用 $("li[class != sel]", this).hide(); 时,在 IE8 中处理性能并且没有收到停止脚本消息。

在 FF 中,这三者都工作得很好。

在 IE7 中,这三个都收到“停止运行脚本”消息。

请帮我解决这个问题。

提前致谢。

编辑:这些 li 是类似 eBay 中的过滤器选项列表。默认情况下,我一开始只显示 5 li。当我单击过滤器下名为“选择更多”的链接时。我打开一个模式窗口并选择复选框中的选项列表,然后在提交时将“sel”类附加到所选的 li 中。最后,在逻辑中,没有“sel”的 li 被隐藏。这些 li 可以大于 400 或更小。取决于数据。用户有权选择所有的li。

感谢您的回复。请给我一些想法:)

最佳答案

嗯,这显然是一个性能问题。我猜这是因为 jQuery 首先收集所有项目的列表,然后再循环它们。

我认为 jQuery 中没有办法避免这种情况,所以我建议不要使用 jQuery,而是使用常规 DOM 方法来循环项目:

var item = this.firstChild; // Assuming "this" is the parent of the list items
while (item) {
$(item).filter(":not(.sel)").hide();
item = item.nextSibling;
}

我再次在循环内使用 jQuery,这更简单,但也更慢。您可能还需要回退到 DOM 方法:

if (item.tagName == "LI" && item.className != "sel") {
item.style.display = "none";
}

关于在 IE 中使用 'not' 的 JQuery 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9062519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com