简体   繁体   English

隐藏/显示多达100个DOM元素的最有效方法是什么?

[英]The most efficient way to hide/show up to 100 DOM elements?

Assume that you have at most 100 elements in which their types and format don't change but their context does. 假设您最多有100个元素,其类型和格式不会改变,但它们的上下文会改变。 (They're basically rows) (他们基本上是行)
These rows around bound to a input and going to change as the user types. 这些行绑定到输入并随着用户键入而更改。

What would be the best approach for maximum performance? 什么是最佳性能的最佳方法? Reusing the elements, keeping all but changing their context? 重用这些元素,保持一切,但改变他们的背景? Anything else? 还要别的吗?

Edit, Clarification: 编辑,澄清:
The search algorithm is unrelated but I do use MVVM (angularjs) framework so the search I'm doing is on JavaScript and is not a bottleneck; 搜索算法是无关的,但我确实使用MVVM(angularjs)框架,所以我正在进行的搜索是在JavaScript上,而不是瓶颈; after getting the results, I update the accordingly. 得到结果后,我相应地更新。

Also I don't need to search for the elements over DOM, I do have the references to elements, I want to minimize the run-time during the update. 此外,我不需要搜索DOM上的元素,我确实有元素的引用,我想在更新期间最小化运行时间。

For the code, this will do, 对于代码,这样做,

$(element).css('display' , 'none');

But the performance issues depends on how you are finding those elements, The key is to wrap elements in to a container, and search for the elements within that container only : 但性能问题取决于您如何找到这些元素, 关键是将元素包装到容器中,并仅搜索该容器中的元素

$('container').find('your_elements').css('display' , 'none');

OR 要么

$('your_elements', 'container').css('display' , 'none');

Will do it. 会做的。

Never do: 永远不会做:

$('your_elements').css('display' , 'none'); 

JS will have to search entire dom for that JS将不得不搜索整个dom

If you have only 100 elements it doesn't really matter. 如果你只有100个元素,那真的不重要。 Just set the display property of their style object to show or hide them. 只需设置其样式对象的display属性即可显示或隐藏它们。

使用所有行的唯一ID并使用jquery通过更改它们的值来操作它。

Best way could be, wrap all those elements in a div and set style to the external div ( assuming they are together as you said they are rows ) 最好的方法是,将所有这些元素包装在div中并将样式设置为外部div(假设它们在一起,就像你说它们是行一样)

<div id="wrapper">
   <!-- Your dom elements -->
</div>

Set proper style to your wrapper div. 为你的包装div设置合适的样式。

And typically I dont think we need to worry about perf on such things as they are very trivial operations. 通常我不认为我们需要担心这些事情的性能,因为它们是非常微不足道的操作。

I think create a CSS class with display none and apply that class to TR elements by doing this you will be able to utilize the jquery selector for later use. 我认为使用display none创建一个CSS类,并通过这样做将该类应用于TR元素,您将能够利用jquery选择器供以后使用。 $('TR.hidden') and if(!$('TD').hasClass('hidden')) $('TR.hidden')if(!$('TD').hasClass('hidden'))

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

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