addClass - can add multiple classes on same div?

前端 未结 4 1208
南方客
南方客 2020-12-08 09:20

jQuery add multiple class

This is my current code, I know its wrong code

$(\'.page-address-edit\').addClass(\'test1\').addClass(\'test2\');


        
相关标签:
4条回答
  • 2020-12-08 09:32
    $('.page-address-edit').addClass('test1 test2 test3');
    

    Ref- jQuery

    0 讨论(0)
  • 2020-12-08 09:34

    You can add multiple classes by separating classes names by spaces

    $('.page-address-edit').addClass('test1 test2 test3');
    
    0 讨论(0)
  • 2020-12-08 09:35

    You code is ok only except that you can't add same class test1.

    $('.page-address-edit').addClass('test1').addClass('test2'); //this will add test1 and test2
    

    And you could also do

    $('.page-address-edit').addClass('test1 test2');
    
    0 讨论(0)
  • 2020-12-08 09:42

    You can do

    $('.page-address-edit').addClass('test1 test2');
    

    More here:

    More than one class may be added at a time, separated by a space, to the set of matched elements, like so:

    $("p").addClass("myClass yourClass");
    
    0 讨论(0)
提交回复
热议问题