JS 给一个li添加样式,同时去掉其他li的样式
2个回答
展开全部
<script type="text/javascript" defer="defer">
function changeStyle(ele){
var liAry=document.getElementById("box").getElementsByTagName("li");
var liLen=liAry.length;
var liID=ele.id;
for(var i=0;i<liLen;i++)
{
if(liAry[i].id==liID)
{
liAry[i].style.backgroundColor="#cccccc";
}
else
{
liAry[i].style.backgroundColor="white";
}
}
}
</script>
<div id="box">
<li id='1' style="background-color:#cccccc;" onclick="changeStyle(this)">AAA</li>
<li id='2' onclick="changeStyle(this)">BBB</li>
<li id='3' onclick="changeStyle(this)">CCC</li>
</div>
function changeStyle(ele){
var liAry=document.getElementById("box").getElementsByTagName("li");
var liLen=liAry.length;
var liID=ele.id;
for(var i=0;i<liLen;i++)
{
if(liAry[i].id==liID)
{
liAry[i].style.backgroundColor="#cccccc";
}
else
{
liAry[i].style.backgroundColor="white";
}
}
}
</script>
<div id="box">
<li id='1' style="background-color:#cccccc;" onclick="changeStyle(this)">AAA</li>
<li id='2' onclick="changeStyle(this)">BBB</li>
<li id='3' onclick="changeStyle(this)">CCC</li>
</div>
展开全部
<style>
.a1{color:red;}
.a2{color:green;}
</style>
<div id="box">
<li id='1' class="a1" >AAA</li>
<li id='2' class="a1">BBB</li>
<li id='3' class="a1">CCC</li>
</div>
<script type="text/javascript" defer="defer">
var liAry=document.getElementById("box").getElementsByTagName("li");
function changeStyle(){
for(var i=0,liLen=liAry.length;i<liLen;i++)
{
if(liAry[i].id==this.id)
{
liAry[i].className="a2"
}else{ liAry[i].className="" }
}
}
for(var i=0,liLen=liAry.length;i<liLen;i++)
{
liAry[i].onclick=changeStyle;
}
</script>
.a1{color:red;}
.a2{color:green;}
</style>
<div id="box">
<li id='1' class="a1" >AAA</li>
<li id='2' class="a1">BBB</li>
<li id='3' class="a1">CCC</li>
</div>
<script type="text/javascript" defer="defer">
var liAry=document.getElementById("box").getElementsByTagName("li");
function changeStyle(){
for(var i=0,liLen=liAry.length;i<liLen;i++)
{
if(liAry[i].id==this.id)
{
liAry[i].className="a2"
}else{ liAry[i].className="" }
}
}
for(var i=0,liLen=liAry.length;i<liLen;i++)
{
liAry[i].onclick=changeStyle;
}
</script>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询