这个JS代码为什么没用?
<scripttype="text/javascript">varnav=document.getElementById("tab-nav");varlis=docume...
<script type="text/javascript">
var nav = document.getElementById("tab-nav");
var lis = document.getElementsByTagName("li");
var divs = document.getElementById("tab-con").document.getElementsByTagName("div");
for(var i=0;i<lis.length;i++){
lis[i].onclick = function(){
for (var i = 0; i < lis.length; i++) {
if(this==lis[i]){
lis[i].className="select";
divs[i].className="show";
}
else{
lis[i].className="";
divs[i].className="";
}
}
</script>
<style type="text/css">
ul,li,div{padding: 0; margin: 0; }
ul li{
float: left;
width: 100px;
height: 30px;
line-height: 30px;
text-align: center;
background-color: #fff;
border: 1px #bbb solid;
border-bottom: none;
}
ul li.select{
background: #ccc;
color: red;
}
ul{
overflow: hidden;
zoom:1;
list-style-type: none;
}
#tab-con{width: 350px;height: 200px;}
#tab-con div{
width: 350px;
height: 200px;
display: none;
border: 1px #bbb solid;
border-top: none;
}
#tab-con div.show{
display: block;
background-color: #ccc;
}
</style>
<div id="zdfz-body">
<ul id="tab-nav">
<li class="select">tab1</li>
<li> tab2 </li>
<li> tab3 </li>
</ul>
<div id="tab-con">
<div class="show ">aaa </div>
<div> bbb</div>
<div> ccc</div>
</div>
这个代码为什么不行呢? 展开
var nav = document.getElementById("tab-nav");
var lis = document.getElementsByTagName("li");
var divs = document.getElementById("tab-con").document.getElementsByTagName("div");
for(var i=0;i<lis.length;i++){
lis[i].onclick = function(){
for (var i = 0; i < lis.length; i++) {
if(this==lis[i]){
lis[i].className="select";
divs[i].className="show";
}
else{
lis[i].className="";
divs[i].className="";
}
}
</script>
<style type="text/css">
ul,li,div{padding: 0; margin: 0; }
ul li{
float: left;
width: 100px;
height: 30px;
line-height: 30px;
text-align: center;
background-color: #fff;
border: 1px #bbb solid;
border-bottom: none;
}
ul li.select{
background: #ccc;
color: red;
}
ul{
overflow: hidden;
zoom:1;
list-style-type: none;
}
#tab-con{width: 350px;height: 200px;}
#tab-con div{
width: 350px;
height: 200px;
display: none;
border: 1px #bbb solid;
border-top: none;
}
#tab-con div.show{
display: block;
background-color: #ccc;
}
</style>
<div id="zdfz-body">
<ul id="tab-nav">
<li class="select">tab1</li>
<li> tab2 </li>
<li> tab3 </li>
</ul>
<div id="tab-con">
<div class="show ">aaa </div>
<div> bbb</div>
<div> ccc</div>
</div>
这个代码为什么不行呢? 展开
展开全部
给你改了下,里面很多大括号没没闭合,js位置给你调整了下,只有加载完html标签才能用js访问元素。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<style type="text/css">
ul,li,div{padding: 0; margin: 0; }
ul li{
float: left;
width: 100px;
height: 30px;
line-height: 30px;
text-align: center;
background-color: #fff;
border: 1px #bbb solid;
border-bottom: none;
}
ul li.select{
background: #ccc;
color: red;
}
ul{
overflow: hidden;
zoom:1;
list-style-type: none;
}
#tab-con{width: 350px;height: 200px;}
#tab-con div{
width: 350px;
height: 200px;
display: none;
border: 1px #bbb solid;
border-top: none;
}
#tab-con div.show{
display: block;
background-color: #ccc;
}
</style>
<div id="zdfz-body">
<ul id="tab-nav">
<li class="select">tab1</li>
<li> tab2 </li>
<li> tab3 </li>
</ul>
<div id="tab-con">
<div class="show ">aaa </div>
<div> bbb</div>
<div> ccc</div>
</div>
<script type="text/javascript">
var nav = document.getElementById("tab-nav");
var lis = document.getElementsByTagName("li");
var divs = document.getElementById("tab-con").getElementsByTagName("div");
for(var i=0;i<lis.length;i++){
lis[i].onclick = function(){
for (var i = 0; i < lis.length; i++) {
if(this==lis[i]){
lis[i].className="select";
divs[i].className="show";
}else{
lis[i].className="";
divs[i].className="";
}
}
}
}
</script>
</body>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<style type="text/css">
ul,li,div{padding: 0; margin: 0; }
ul li{
float: left;
width: 100px;
height: 30px;
line-height: 30px;
text-align: center;
background-color: #fff;
border: 1px #bbb solid;
border-bottom: none;
}
ul li.select{
background: #ccc;
color: red;
}
ul{
overflow: hidden;
zoom:1;
list-style-type: none;
}
#tab-con{width: 350px;height: 200px;}
#tab-con div{
width: 350px;
height: 200px;
display: none;
border: 1px #bbb solid;
border-top: none;
}
#tab-con div.show{
display: block;
background-color: #ccc;
}
</style>
<div id="zdfz-body">
<ul id="tab-nav">
<li class="select">tab1</li>
<li> tab2 </li>
<li> tab3 </li>
</ul>
<div id="tab-con">
<div class="show ">aaa </div>
<div> bbb</div>
<div> ccc</div>
</div>
<script type="text/javascript">
var nav = document.getElementById("tab-nav");
var lis = document.getElementsByTagName("li");
var divs = document.getElementById("tab-con").getElementsByTagName("div");
for(var i=0;i<lis.length;i++){
lis[i].onclick = function(){
for (var i = 0; i < lis.length; i++) {
if(this==lis[i]){
lis[i].className="select";
divs[i].className="show";
}else{
lis[i].className="";
divs[i].className="";
}
}
}
}
</script>
</body>
展开全部
把js写在body下边 ,因为js运行的时候 dom树没有加载好,或者用window。onload包裹起来
追问
依然没用
追答
Title
tab1
tab2
tab3
aaa
bbb
ccc
var nav = document.getElementById("tab-nav");
var lis = document.getElementsByTagName("li");
var divs = document.querySelectorAll("div");
console.log(lis,divs);
for(var i=0;i
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询