日期:2014-05-17  浏览次数:20940 次

请教一段脚本程序:怎么在点击一个<li>xxx</li>的内容时,让该内容的背景颜色变化
请教一段脚本程序:怎么在点击一个 <li> xxx </li> 的内容时,让该内容的背景颜色变化

------解决方案--------------------
<li onmousedown= 'this.style.backgroundColor= "#FF0000 "; '> xxx </li>
------解决方案--------------------
给该对象添加onclick事件,操作该对象的style对象,如:

<li onclick= "this.style.backgroundColor= 'red ' "> test </li>
------解决方案--------------------
这需要一个变量用来记录当前对象,如:

<script type= "text/javascript ">
var nowobj = null;
function setBGC(obj){
if(nowobj!=null){
nowobj.style.backgroundColor = " ";
}
obj.style.backgroundColor = "red ";
nowobj = obj;
}
</script>

<li onclick= "setBGC(this); "> x1 </li> <li onclick= "setBGC(this); "> x2 </li> <li onclick= "setBGC(this); "> x3 </li>
------解决方案--------------------
给 <li> 不同的标识