Jquery hide() 和 show() 在 google chrome 中运行太慢

发布于 2024-10-15 05:38:30 字数 516 浏览 0 评论 0原文

我有一个 Web 应用程序在 Chrome 中无法正常运行。在 Firefox 中完美运行。我有一个包含大量列表项的页面,确切地说是 316 个。每个列表项都包含大量 HTML。我的问题是当我想隐藏或显示这些列表项时。

我在 jsFiddle 上有一个测试页面来显示我遇到的问题。我将 HTML 页面精简为一个无序列表来保存所有 316 个列表项。我有两个按钮,单击时只需调用 jQuery 隐藏或显示。同样,它在 Firefox、Opera 甚至 IE 中运行得很快,在 Safari 中也运行得很好,但在 Google Chrome 中可能需要 30 秒以上,这会弹出对话框窗口,询问您是否要终止页面,因为脚本运行时间太长。

的链接

这是 jsFiddle http://jsfiddle.net/oumichaelm/UZCZc/3/ 嵌入/结果/

感谢您的任何意见。 杰姆

I have a web application that doesn't run correctly in chrome. Works perfectly in Firefox. I have a page with a large numbered of list items, 316 to be exact. Each list item contains a large amount of HTML. My problem is when I want to hide or show these list items.

I have a test page on jsFiddle to show the problem I'm having. I stripped down the HTML page to one unordered list to hold all 316 list items. I have two buttons that simply call jQuery hide or show when clicked. Again this runs fast in Firefox, Opera, even IE, pretty well in Safari but in Google Chrome it can take over 30 seconds which brings up the dialog window asking if you want to kill the page because a script is running to long.

Here is the link to jsFiddle

http://jsfiddle.net/oumichaelm/UZCZc/3/embedded/result/

thanks for any input.
jmm

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

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

发布评论

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

评论(3

一曲琵琶半遮面シ 2024-10-22 05:38:30

看起来这与 jQuery 无关,只是 Chrome 隐藏具有大量子元素的父元素的问题。

这只是使用基本的 JavaScript 来隐藏文档准备好的元素:

document.getElementById('sortable-lines').style.display="none";

并且在文档准备好后仍然需要很长时间。

http://jsfiddle.net/petersendidit/UZCZc/10/embedded/result/

为此打开了一个 Chrome 错误:http://code.google。 com/p/chromium/issues/detail?id=71305

Looks like this has nothing to do with jQuery and just is a problem with Chrome hiding an parent element that has a HUGE number of children elements.

This just uses basic javascript to hide the element on document ready:

document.getElementById('sortable-lines').style.display="none";

And it still takes forever after the document is ready.

http://jsfiddle.net/petersendidit/UZCZc/10/embedded/result/

Opened a Chrome bug for this: http://code.google.com/p/chromium/issues/detail?id=71305

亽野灬性zι浪 2024-10-22 05:38:30

隐藏时,从 DOM 中删除元素比使用 hide() 更快。

var sortableLines = $('#sortable-lines');
$('#hide').click(function() {
    $('#timer').text("Hiding");        
    sortableLines.remove();
});

当你 append() 将它返回到 DOM 时,它仍然很慢。

一种可能的解决方法是在单击显示按钮时显示前 10 个左右的项目,然后使用 setInterval 逐步显示它们。


编辑:发现另一个黑客:

您必须将容器设置为overflow:hidden

#linecontainer { overflow: hidden; }

隐藏时,通过设置margin-top将该元素移动到最顶部 为一个大负数。

$('#hide').click(function() {
    $('#timer').text("Hiding");
    sortableLines.css('margin-top', '-1000000px');
});

显示时,重置其margin-top

$('#show').click(function() {
    $('#timer').text("Showing");
    sortableLines.css('margin-top', '0');
});

并且它立即显示和隐藏

When hiding, removing the element from the DOM is faster than using hide().

var sortableLines = $('#sortable-lines');
$('#hide').click(function() {
    $('#timer').text("Hiding");        
    sortableLines.remove();
});

It is still slow when you append() it back to the DOM.

A possible workaround is to show the first 10 or so items when the show button is clicked, and then setInterval to progressively show them.


Edit: Found another hack:

You have to set the container to overflow: hidden:

#linecontainer { overflow: hidden; }

When hiding, move that element up to far top, by setting margin-top to a big negative number.

$('#hide').click(function() {
    $('#timer').text("Hiding");
    sortableLines.css('margin-top', '-1000000px');
});

When showing, reset its margin-top.

$('#show').click(function() {
    $('#timer').text("Showing");
    sortableLines.css('margin-top', '0');
});

And it shows and hides instantly.

許願樹丅啲祈禱 2024-10-22 05:38:30

感谢上面的回答,它效果很好并且加快了进程。

然而它并不总是有效 - 当我需要的元素位于列表顶部时效果很好。但是,如果我从列表中间选择某些内容,它不会显示全部内容。

我相信我知道它为什么行为不端。

当一长串元素的值设置为隐藏/显示时,隐藏元素将从流中删除,并按照删除的顺序放置在页面底部。
这使得删除元素的速度非常快。

然而,尝试使它们再次可见对于渲染来说是一件痛苦的事情,因为 chrome 必须记住这些项目所属的顺序,并且似乎要重新计算关联的值。

与大多数其他浏览器不同,该组件的位置不会丢失,因此不会在这种不必要的排序中浪费时间。上面的答案非常有效,因为这避免了 Chrome 的无序问题。

Thanks for the answer above, it works great and speeds up the process.

It doesn't however always work - Works nice when the elements I need are at the top of the list. However, it doesn't show them all if I pick something from the middle of the list.

I believe I know why it misbehaves.

When the value of a long list of elements is set to hide/show the hidden elements are removed from the flow and placed on the bottom of the page in the order they were removed.
This makes removing the elements remarkably fast.

However, trying to make them visible again is a pain on the rendering as chrome has to remember the order in which these items belonged in and seemingly to recompute the values associated.

Other than most other browsers the spot for the component isn't lost so no time is wasted in this unnecessary sorting. The above answer works remarkably well as this avoids Chrome's disordering problem.

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