JS代码:
- JScript code
var lis=document.getElementsByTagName("li"); function Change(obj){ for(var i=1;i<lis.length;i++){ if(lis[i].onmouseover==null && lis[i].onmouseout==null){ lis[i]=this; lis[i]. onmouseover=function(){this.style.background='#FAFAFA'}; lis[i].onmouseout=function(){this.style.background=''}; getFirstChild(lis[i]).style.visibility="hidden"; } if(lis[i].id == obj.id){ obj.style.background='blue'; obj.onmouseover=""; obj.onmouseout=""; getFirstChild(obj).style.visibility="visible"; }else{ lis[i].style.background=''; } } } //获取指定节点的第一个子节点 function getFirstChild(obj) { var result = obj.firstChild; while (!result.tagName) { result = result.nextSibling; } return result; }
前台代码:
- HTML code
<div id="play_list"> <div class="divScroll"> <ul style="list-style:none"> <li class="pl-title">播放列表:</li> <?php for($i=0;$i<30;$i++){ ?> <li id="pl-<?php echo $i ?>" onmouseover="this.style.background='#FAFAFA';" onmouseout="this.style.background='';" onclick="Change(this);"> <img class="pl-img" src="../images/music.gif" /> <a href="#">Katy Perry - The One That Got Away</a> </li> <?php } ?> </ul> </div> </div>
------解决方案--------------------
var lis=document.getElementsByTagName("li");
function Change(obj){
放在
function Change(obj){
var lis=document.getElementsByTagName("li");
确保调用的时候对象已经存在