0
<style type="text/css">

.web_index div {width: 400px; height: 300px; background: #eee;}
    ul li{width: 100px; height: 30px; line-height: 30px; list-style: none; display: inline-block; *display: inline; zoom: 1;}
</style>
<script type="text/javascript">
function licker(){
var lier=document.getElementsByTagName("li");
var diver=document.getElementsByClassName("web_index")[0].getElementsByTagName("div");
for(var i=0;i<lier.length;i++)
    { 
        for(j=0;j<diver.length;j++)
        {
            if(i==j)
            {
             diver[j].style.display=block;
                }
            else{
                diver[j].style.display=none;
                }   
            }
    }
}

</script>

html:

<ul>
    <li onclick="licker()" class="li01">the first li</li>
    <li onclick="licker()" class="li02">the second li</li>
    <li onclick="licker()" class="li03">the third li</li>
    <div class="web_clear"></div>
</ul>
<div class="web_index">
<div style="display:block" >content one</div>
<div style="display:none">content two</div>
<div style="display:none">content three</div>
</div>

单击第一个li时,显示内容一,其他全部隐藏,单击第二个li时,显示内容二。其他的都被隐藏了......为什么我的代码不起作用。如何更正它。谢谢,

4

2 回答 2

1

您应该为您li的 's 分配一个索引:

<ul>
    <li onclick="licker(0)" class="li01">the first li</li>
    <li onclick="licker(1)" class="li02">the second li</li>
    <li onclick="licker(2)" class="li03">the third li</li>
    <div class="web_clear"></div>
</ul>

和你的功能:

function licker(id){
    var diver=document.getElementsByClassName("web_index")[0].getElementsByTagName("div");
    for(var i=0;i<diver.length;i++){ 
        if(i==id){
            diver[i].style.display='block';
        } else {
            diver[i].style.display='none';
        }   
    }
}​

JSFiddle 示例

于 2012-09-14T07:38:39.860 回答
1

试试这个

<ul>
   <li onclick="licker(this);">the first li</li>
   <li onclick="licker(this);">the second li</li>
   <li onclick="licker(this);">the third li</li>
</ul>
<div class="web_index">
   <div style="display: block">content one</div>
   <div style="display: none">content two</div>
   <div style="display: none">content three</div>

function licker(sender) {
    var lier = document.getElementsByTagName("li");
    var diver = document.getElementsByClassName("web_index")[0].getElementsByTagName("div");
    for (var i = 0; i < lier.length; i++) {
       if (lier[i] != sender) continue;
       for (j = 0; j < diver.length; j++) {
          if (i == j)
             diver[j].style.display = 'block';
          else 
              diver[j].style.display = 'none';

          }
       }
    }
</script>
于 2012-09-14T07:47:44.140 回答