<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>className</title> <style type="text/css"> li{ list-style:none;} .box{ width:100px; height:100px; line-height:100px; background:#CC3300; margin:4px; color:#fff; text-align:center;} .green{ background:#009900;} .selected{ background:#9900FF;} </style> <script type="text/javascript"> window.onload=function(){ var lis=document.getElementsByTagName('li'); var i=0; for(i=0; i<lis.length; i++){ lis[i].onmouseover=function(){ this.className+=' selected'; }; lis[i].onmouseout=function(){ var oldClassName=this.className; var oldClassNameArray=oldClassName.split(' '); oldClassNameArray.pop(); var nowClassName=oldClassNameArray.join(' '); this.className=nowClassName; }; } } </script> </head> <body> <ul> <li class="box">1</li> <li class="box green">2</li> <li class="box cc">3</li> <li class="box dd">4</li> </ul> </body> </html>
转载于:https://www.cnblogs.com/homework/archive/2012/04/16/2452882.html