How do I add a class to a given element?

前端 未结 25 2452
清酒与你
清酒与你 2020-11-21 11:34

I have an element that already has a class:

25条回答
  •  我寻月下人不归
    2020-11-21 12:09

    Just to elaborate on what others have said, multiple CSS classes are combined in a single string, delimited by spaces. Thus, if you wanted to hard-code it, it would simply look like this:

    From there you can easily derive the javascript necessary to add a new class... just append a space followed by the new class to the element's className property. Knowing this, you can also write a function to remove a class later should the need arise.

提交回复
热议问题