Manipulating style with getElementsByClassName

后端 未结 5 1105
名媛妹妹
名媛妹妹 2021-01-27 10:25

I don\'t understand why I cannot manipulate the style of .special in my code. I\'m sure it\'s something simple, but it\'s not working.

I am an h1!<

5条回答
  •  长情又很酷
    2021-01-27 10:59

    document.getElementsByClassName returns an array-like object. You need to reference it as such.

    var x = document.getElementsByClassName("special")[0]; //Get the first element with the class name
    x.style.color = "blue";
    

提交回复
热议问题