使用 JavaScript 点击显示/隐藏 Div
2014-11-07
121
我尝试使用 Javascript 在点击时显示/隐藏标签,但出现错误(“未捕获 TypeError:无法设置未定义标签的属性‘className’。(匿名函数)。onclick”)。有人能告诉我问题可能出在哪里吗?
<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>
3个回答
您的内部 for 循环具有一个 i 变量,该变量与具有相同名称的外部 for 循环变量冲突。
您还应该在将单击的元素设置为“selected”之前从所有元素中删除 selected 类。
尝试:
<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() {
for (var j = 0; j < panels.length; j++) {
tabs[j].className = panels[j].className = "";
}
tabs[i].className = panels[i].className = "selected";
}
}(i);
}
</script>
André Teixeira
2014-11-07
您遇到了几个问题:
-
多个
i
变量 -
new function(i) {...
不是最佳语法。我在下面使用了闭包 - 每行多次赋值并不好
我已为您的
<li>
元素赋值,以便我们可以判断哪个 li 元素已被点击
var tabs = document.getElementById("tabs").getElementsByTagName("li");
var panels = document.getElementById("panels").getElementsByTagName("div");
for (var i = 0; i < panels.length; i++) {
(function(i) {
tabs[i].onclick = function() {
var j;
var panelIndex;
// remove styles from other tabs
for (j = 0; j < tabs.length; j++) {
tabs[j].className = "";
}
// apply style to the current tab: 'this'
this.className = "selected";
// hide other panels
for (j = 0; j < panels.length; j++) {
panels[j].className = "";
}
// show the selected panel
panelIndex = +this.value; // convert value to number
panels[panelIndex-1].className="selected"; // arrays are 0-indexed, so subtract 1
}
})(i);
}
a { text-decoration: none; }
li { list-style: none; }
li.selected { font-weight: bold; }
.panels div { display: none; }
.panels .selected { display: block; }
<div id="tabs" class="tabs">
<ul>
<li value="1" class="selected"><a href="javascript:;">One</a></li>
<li value="2" class=""><a href="javascript:;">Two</a></li>
<li value="3" 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>
jasonscript
2014-11-07
下面将按预期工作。我发现在 for 循环内访问 HTML 元素有两个问题,您需要使用 .item() 作为要获取的 HTMLCollection,而不是数组。此外,您的内部 for 循环需要使用不同的循环索引,并附加一个 if 条件,使单击的元素保持显示状态,其余元素隐藏。
<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.item(i).className = panels.item(i).className = "selected";
for (var j = 0; j < panels.length; j++) {
if(i!=j){
tabs.item(j).className = panels.item(j).className = "";
}
}
}
}(i);
}
</script>
Gaurav Daga
2014-11-07