How to use JavaScript to change div backgroundColor

前端 未结 9 2033
萌比男神i
萌比男神i 2020-11-29 05:26

The HTML below:

some title here

some content here

9条回答
  •  孤城傲影
    2020-11-29 06:01

    var div = document.getElementById( 'div_id' );
    div.onmouseover = function() {
      this.style.backgroundColor = 'green';
      var h2s = this.getElementsByTagName( 'h2' );
      h2s[0].style.backgroundColor = 'blue';
    };
    div.onmouseout = function() {
      this.style.backgroundColor = 'transparent';
      var h2s = this.getElementsByTagName( 'h2' );
      h2s[0].style.backgroundColor = 'transparent';
    };
    

提交回复
热议问题