Home > Web Front-end > JS Tutorial > body text

JavaScript study notes (2) Code to change the background color of the div block when the mouse passes over_Basic knowledge

WBOY
Release: 2016-05-16 18:08:05
Original
1210 people have browsed it

HTML part of the code:

Copy code The code is as follows:


    < ;li>Change the background color when the mouse passes over
  • Change the background color when the mouse passes over

  • Change the background color when the mouse passes over

  • Change the background color when the mouse passes over



Javascript part of the code:
When the mouse passes over, add li class="current" class, when the mouse leaves, remove this class to achieve the purpose of changing the background color
Copy the code The code is as follows:

window.onload = function() {
var lis = document.getElementsByTagName("li");
for (var i=0; ilis[i].onmouseover = function() {
this.setAttribute("class", "current");
}
lis[i].onmouseout = function() {
this.setAttribute("class", "");
}
}
}

Css part code:
Copy code The code is as follows:

ul li.current {
background-color:red;
cursor:pointer;
}

The above code is mainly to facilitate everyone to know more about js. A css-implemented version is provided below.

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute ]
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template