JavaScript access CSS class by its name?

后端 未结 6 1864
难免孤独
难免孤独 2021-01-12 16:33

I have to access CSS class by name, and the code below works. However if I try hui[\"myclass\"] or hui[\".myclass\"] instead of hui[0]

6条回答
  •  遥遥无期
    2021-01-12 16:46

    To access the class from javascript you just need to create a classist, if it's not there.

    document.querySelector("myDiv").classList.add("className");
    

    This adds the new class to the div with the specified class name.

    Else:

    document.querySelector(".className");
    

提交回复
热议问题