老师帮忙解释下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#content{
width:300px;
height:260px;
border: 1px solid #3bb4ff;
margin-top: 100px;
position: relative;
}
#content div{
width:80px;
height:35px;
color: white;
background: blue;
line-height: 35px;
text-align: center;
border-radius: 5px 5px 0 0;
border-right:1px solid #3bb4ff;
border-left:1px solid #3bb4ff;
border-top:1px solid #3bb4ff;
position: absolute;
top:-36px;
}
#content #bt0{ left:15px; }
#content #bt1{ left:100px; }
#content #bt2{ left:185px; }
#content .box1{background: white; color:black;}
#content .box{margin-left: 10px; color: black;
font-size:18px;
font-weight: bold; }
</style>
</head>
<body>
<div id="content">
<div class="ind" id="bt0">DOM0</div>
<div class="ind" id="bt1">DOM1</div>
<div class="ind" id="bt2">jQuery</div>
<p></p>
</div>
<script type="text/javascript">
var a=document.getElementById('bt0');
var b=document.getElementById('bt1');
var c=document.getElementById('bt2');
var ind=document.getElementsByClassName('ind');
var p=document.getElementsByTagName('p')[0];
var n=ind.length;
console.log(ind.length)
a.onclick=function(){
for(var i=0;i<ind.length; i++){
ind[i].style.background="blue";
ind[i].style.color="white" }
this.style.background="white";
this.style.color="black"
}
b.onclick=function(){
for(var i=0;i<ind.length; i++){
ind[i].style.background="blue";
ind[i].style.color="white" }
this.style.background="white";
this.style.color="black"
}
c.onclick=function(){
for(var i=0;i<ind.length; i++){
ind[i].style.background="blue";
ind[i].style.color="white" }
this.style.background="white";
this.style.color="black"
}
</script>
</body>
</html>
为什么下面代码这样直接elements.style.className="" 而无法实现效果?
<script type="text/javascript">
var a=document.getElementById('bt0');
var b=document.getElementById('bt1');
var c=document.getElementById('bt2');
var ind=document.getElementsByClassName('ind');
var p=document.getElementsByTagName('p')[0];
var n=ind.length;
a.onclick=function(){
for(var i=0;i<ind.length; i++){
ind[i].className="";
}
this.className="box1";
}
b.onclick=function(){
for(var i=0;i<ind.length; i++){
ind[i].className="";
}
this.className="box1";
}
c.onclick=function(){
for(var i=0;i<ind.length; i++){
ind[i].className="";
}
this.className="box1";
}
</script>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 11218 份
- 解答问题 36712 个
从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星