How do I change an elements class in jquery?

前端 未结 4 1266
情话喂你
情话喂你 2021-01-15 05:36

Assuming I have

    how can I change the value of verticalList using jquery?

4条回答
  •  迷失自我
    2021-01-15 06:24

    There are several functions that you can use to manipulate an element class.

    toggleClass adds a class on the element if it doesn't exists. If it exists then it's removed.

    $(".myList").toggleClass("verticleList");
    

    addClass insert the class on the element. If the class already exists it does nothing.

    $(".myList").addClass("aClass");
    

    removeClass remove the class from the element. If the class doesn't exists it does nothing.

    $(".myList").removeClass("aClass");
    

    hasClass returns true or false if the element has or not a class.

    $(".myList").hasClass("aClass");
    

    You can also change the class modifying the attribute (.attr) class which may not be recommended.

    • See the example on jsFiddle.
    • Reference of class functions on jQuery API.

提交回复
热议问题