我試圖用Javascript顯示/隱藏點擊標籤,但我得到錯誤(「Uncaught TypeError:無法設置未定義標籤的屬性'className'(匿名函數) .onclick「)。有人能告訴我這個問題可能是什麼嗎?用Javascript點擊顯示/隱藏Div點擊
<style>
a { text-decoration: none; }
li { list-style: none; }
li.selected { font-weight: bold; }
.panels div { display: none; }
.panels .selected { display: block; }
</style>
<div id="tabs" class="tabs">
<ul>
<li class="selected"><a href="javascript:;">One</a></li>
<li class=""><a href="javascript:;">Two</a></li>
<li class=""><a href="javascript:;">Three</a></li>
</ul>
</div>
<div id="panels" class="panels">
<div class="selected">This is panel one.</div>
<div class="">This is panel two.</div>
<div class="">This is panel three.</div>
</div>
<script>
var tabs = document.getElementById("tabs").getElementsByTagName("li");
var panels = document.getElementById("panels").getElementsByTagName("div");
for (var i = 0; i < tabs.length; i++) {
new function(i) {
tabs[i].onclick = function() {
tabs[i].className = panels[i].className = "selected";
for (var i = 0; i < panels.length; i++) {
tabs[i].className = panels[i].className = "";
}
}
}(i);
}
</script>
也許你想試試Jquery .. – 2014-11-07 02:16:48