Benefits of using attr() over addClass in jquery

后端 未结 5 1681
野性不改
野性不改 2020-12-03 08:07

What are the benefits of using attr() instead of using addClass() in jquery?

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 08:52

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

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

    for example...

    Html

    Script

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

    Result

    And...

    Html

    Script

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

    Result

    jQuery Links:

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

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

提交回复
热议问题