Javascript learning note (2) when the mouse passes, the background color of the div block is changed

Source: Internet
Author: User
HTML code: viewsourceprint? 1 & lt; ul & gt; & nbsp; 2 & nbsp; & lt; li & gt; change the background color when the mouse passes through & lt; /li & gt; & nbsp; 3 & nbsp; & lt; li & gt; change the background color when the mouse passes through & lt; /li & gt;

Some HTML code:
View sourceprint? 1

    2

  • Changes the background color when the mouse passes
  • 3

  • Changes the background color when the mouse passes
  • 4

  • Changes the background color when the mouse passes
  • 5

  • Changes the background color when the mouse passes
  • 6

Javascript code:
When the mouse passes, add the class = "current" to li. When the mouse leaves, remove the class to change the background color!
View sourceprint? 01 window. onload = function (){

02 var lis = document. getElementsByTagName ("li ");

03 for (var I = 0; I

04 lis [I]. onmouseover = function (){

05 this. setAttribute ("class", "current ");

06}

07 lis [I]. onmouseout = function (){

08 this. setAttribute ("class ","");

09}

10}

11}

Part of the css code:
View sourceprint? 1 ul li. current {

2 background-color: red;

3 cursor: pointer;

4}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.