jQuery性能,.css或addClass [英] jQuery performance, .css or addClass
问题描述
我有一个大的jQuery代码,我在思考我的功能的速度性能。当一个元素被点击(mousedown),我需要分配一个图像作为背景。我可以通过两种方式:
$('#element li.class')。css({background: someimageURL});
或
$('#element li.class')。addClass(someclass);
其中someclass有实际的CSS背景图片。
有没有办法测试各种功能的速度?
谢谢 $ .addClass()
通过<$设置一对css规则c $ c> $。css()可能没有什么可担心的,但如果你发现自己设置了很多,通常是时候创建一个类,并根据需要应用/删除。 >
我已将两种方法的逻辑提取到一个位置,供您查看是否喜欢。
I have a big jQuery code and I am thinking at speed performance of my functions. When an element is clicked (mousedown) i need to assign an image as background. I can do this by 2 ways:
$('#element li.class').css({"background":"someimageURL"});
or
$('#element li.class').addClass("someclass");
where "someclass" have the actual CSS background image. Witch function works better in this case.
Is there a way to test various functions speed?
Thank you
I'm almost certain .addClass()
would be the faster of the two. This involves essentially tacking on another classname to the element, whereas the alternative would require iterating through the elements styles and setting many explicit rules.
Setting a couple css rules via $.css()
is likely nothing to worry about, but if you find yourself setting many, often, it's time to create a class and apply/remove that as needed.
I've extracted the logic of both methods into a single location for you to review if you like.
这篇关于jQuery性能,.css或addClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!