Beneficios de usar attr () sobre addClass en jquery

¿Cuáles son los beneficios de usar attr() En lugar de usar addClass() en jquery?

preguntado el 01 de febrero de 12 a las 04:02

Performance, while a veces important, is not the sole deciding factor of what's "good" or "bad". The question presumably asks about benefits of any type, not just speedwise. -

For me it is addClass that is faster on jsperf.com... It's good cause I always used addClass I was scared to change everything :-p -

5 Respuestas

Cuando se utiliza attr method to set the class of any element it will override the existing class name with whatever value you pass as the second argument to attr método.

Donde como si usaras addClass method to add any class to an element it will just add the class and also retain the existing classes. If the class to add already exists then it will just ignore it.

P.ej

<div id="div1" class="oldClass"></div>

Usar $('#div1').attr('class', 'newClass') daré

<div id="div1" class="newClass"></div>

Where as using $('#div1').addClass('newClass') daré

<div id="div1" class="oldClass newClass"></div>

So it is always adviseable to use addClass/removeClass to manupulate classes of html elements using jQuery. But at the end it depends on your requirement what to use when.

Respondido el 13 de junio de 13 a las 13:06

addClass() has several benefits over manipulating the class with attr('class'):

  • It's semantically clearer. addClass y la removeClass manipulate the element's class atributo (o className property), and that's about it. It's a little more difficult for them to make the code lie to you. If you said $whatever.addClas("selected"), you'd get a runtime error, whereas if you said $whatever.attr('clas', 'selected'), it just wouldn't appear to do anything. It'd still "work" from JS's point of view, though, because who knows? You could quieres establecer un clas attribute. That's the kind of thing that happens when you try to use a Swiss army knife as a can opener.

  • It works with multiple classes. Si utiliza attr to add and remove CSS classes, and there will ever be more than one (which is rather common, actually), you'd have to do some string processing to make sure not to disturb any other applied classes or have the same class repeated 50 times. addClass y la removeClass haz eso por ti.

  • It's apparently a lot faster.

I can't really think of any benefits of attr('class'...) encima addClass. Generally, attr is for when you don't have some other built-in way to manipulate the attribute. In this case you do, so you should probably use that.

Respondido 01 Feb 12, 09:02

attr(a,b) sets an element's "a" attribute to "b".

addClass(a) adds the "a" class to an element

por ejemplo...

html

<div id="box" class="myClass"></div>

Guión

$("#box").attr("class", "myOtherClass");

Resultado

<div id="box" class="myOtherClass"></div>

Y...

html

<div id="box" class="myClass"></div>

Guión

$("#box").addClass("myOtherClass");

Resultado

<div id="box" class="myClass myOtherClass"></div>

jQuery Links:

http://api.jquery.com/attr/

http://api.jquery.com/addClass/

Respondido 01 Feb 12, 09:02

.addClass() is MUCH better for performance.

Check out this guide to jQuery performance: http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/

One of my favorite articles.

Respondido el 15 de Septiembre de 14 a las 13:09

El único beneficio de attr() I know if is that it works with SVG, addClass() no.

This answer has some details https://stackoverflow.com/a/10257755/2393562

contestado el 23 de mayo de 17 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.