为什么要document.getElementById("ul")[0]呢?不是只有一个<ul>标签吗?奇怪的是去掉[0]之后获取不到<ul>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>实践题 - 选项卡</title>
<style type="text/css">
/* CSS样式制作 */
*{padding:0px;margin: 0px;font:12px normal "microsoft yahei";}
#table{height:200px;width: 300px;}
#table ul{border-bottom:2px solid brown;list-style: none;height: 30px;}
#table ul li{cursor:pointer;float:left;list-style:none;height:28px;line-height:28px;margin:0px 3px;border:1px solid #aaaaaa;border-bottom:none;display:inline-block;width:60px;text-align: center;}
#table ul li.active{border-top:2px solid brown;border-bottom: 2px solid #fff;}
#table div{height:170px;line-height: 40px;border: 2px solid brown;border-top:none;}
.hide{display:none;}
</style>
<script type="text/javascript">
window.onload = function() {
var table=document.getElementById("table");
var ul=table.getElementsByTagName("ul")[0];
var li=ul.getElementsByTagName("li");
var div=table.getElementsByTagName("div");
for(var i=0,len=li.length;i<len;i++)
{
li[i].index=i;
li[i].onclick=function()
{
for(var j=0;j<len;j++)
{
li[j].className="";
div[j].className="hide";
}
this.className="active";
div[this.index].className="";
}
}
}
</script>
</head>
<body>
<!-- HTML页面布局 -->
<div id="table">
<ul>
<li class="active">1</li><li>2</li><li>3</li>
</ul>
</div>
</body>
</html>