亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么要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>


正在回答

1 回答

getElementsByTagName 的返回值是個數組,數組里面只有一個元素,訪問數組元素要用數組的下標,第一個元素的數組下表index 就是0

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么要document.getElementById("ul")[0]呢??不是只有一個<ul>標簽嗎?奇怪的是去掉[0]之后獲取不到<ul>

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號