English 中文(简体)
j 质量——为什么通过扼制(<div category=“foo”/>)来制造同级物体,比制造同样的物体和执行添加标记法要慢?
原标题:jQuery - Why creating object with class by string ($( <div class="foo" /> )) is slower than creating same object and execute addClass() method?

Can anyone may explain me why creating object via string is slower than same object and execute addClass() method in jQuery?

我认为,添加集束(Clas)的方法将比较缓慢,但不是这样。 我很想知道为什么?

http://jsperf.com/jquery-append-with-class-and-with-method-addclass” http://jsperf.com/jquery-append-with-class-and-method-addem

最佳回答

That s because only passing an element name, like $("<div>"), maps to a call to document.createElement().

另一方面,通过一个要素及其属性,例如$><div category= foo >”,向下列电话发送地图:

问题回答

我要说的是,$( <div category=“foo”/>>是因为必须穿透超文本,然后执行addClass((或内部对等)。

我尝试增加第3个测试案例。

viaObject = $("<div>", { class: "foo-"+counterN });
biz.append(viaObject);
counterN++;

i 由于Frédéric Hamidi(文件.createElement() vs document.createDocumentFragment())指出的原因,这种想法应当像(“<div>”;一样快,但速度仍然缓慢。

或许可以增加的Class()比获得物体的特性更快。

无论如何,这证明,你应当创造你这样的要素。

 $("<div>", { class: "foo-"+counterN }); 

并且不喜欢:

 $( <div class="foo-  + counterS +  " /> );




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签