JavaScript style.display="none" 或 jQuery .hide() 效率更高?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13688238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:40:35  来源:igfitidea点击:

JavaScript style.display="none" or jQuery .hide() is more efficient?

javascriptjquerycssperformance

提问by Maninda

document.getElementById("elementId").style.display="none"

is used in JavaScript to hide an element. But in jQuery,

在 JavaScript 中用于隐藏元素。但是在 jQuery 中,

$("#elementId").hide();

is used for the same purpose. Which way is more efficient? I have seen a comparison between two jQuery function .hide()and .css("display","none")here.

用于相同的目的。哪种方式效率更高?我已经看到了两个 jQuery 函数.hide().css("display","none")这里的比较。

But my problem is whether pure JavaScript is more efficient than jQuery?

但我的问题是纯 JavaScript 是否比 jQuery 更有效?

回答by jAndy

Talking about efficiency:

谈效率:

document.getElementById( 'elemtId' ).style.display = 'none';


What jQuery does with its .show()and .hide()methods is, that it remembers the last stateof an element. That can come in handy sometimes, but since you asked about efficiency that doesn't matter here.

jQuery 用它的.show().hide()方法做的是,它记住元素的最后状态。这有时会派上用场,但既然你问的效率在这里并不重要。

回答by Florian Margaine

a = 2;

vs

对比

a(2);
function a(nb) {
    lot;
    of = cross;
    browser();
    return handling(nb);
}

In youropinion, what do you think is going to be the fastest?

看来,您认为最快的是什么?

回答by Jamund Ferguson

Efficiency isn't going to matter for something like this in 99.999999% of situations. Do whatever is easier to read and or maintain.

在 99.999999% 的情况下,效率对于这样的事情来说并不重要。做任何更容易阅读和/或维护的事情。

In my apps I usually rely on classes to provide hiding and showing, for example .addClass('isHidden')/.removeClass('isHidden')which would allow me to animate things with CSS3 if I wanted to. It provides more flexibility.

在我的应用程序中,我通常依靠类来提供隐藏和显示,例如.addClass('isHidden')/.removeClass('isHidden'),如果我愿意,我可以使用 CSS3 为事物设置动画。它提供了更多的灵活性。

回答by Naftali aka Neal

Yes.

是的。

Yes it is.

是的。

Vanilla JSis always more efficient.

Vanilla JS总是更高效。