点击即可使用Javascript显示/隐藏Div

时间:2014-11-07 02:08:59

标签: javascript

我试图仅使用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 个答案:

答案 0 :(得分:1)

你的内部for循环有一个i变量,它与具有相同名称的循环的outter变量冲突。

在设置点击后的元素&#39;选择&#39;之前,您还应该从所有元素中删除所选的类。

尝试:

<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>

答案 1 :(得分:0)

正如您所料,下面的内容将会有效。我在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>

答案 2 :(得分:0)

你有几个问题:

  • 多个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>