简单的点击式Javascript下拉导航,无需使用开关/tab

Simple Onclick Javascript Drop Down Navigation without using switch / tab

本文关键字:开关 tab 导航 Javascript 简单      更新时间:2023-09-26

使用Javascript还很新鲜。希望避免使用Jquery或其他框架。

这是一个简单的下拉导航,我正试图使用我在这里找到的脚本创建:http://blog.movalog.com/a/javascript-toggle-visibility/

我需要一些帮助来清理一些代码,并获取一些指针。它按我需要的方式工作,但我想把它缩短一点。谢谢

HTML:

<div id="dropMenu">
    <ul>
        <li><a href="#" onclick="showhide1(d1);">Advanced AGM</a>
        </li>
        <li><a href="#" onclick="showhide2(d2);">Lithium-ION</a>
        </li>
        <li><a href="#" onclick="showhide3(d3);">Chargers</a>
        </li>
        <li><a href="#" onclick="showhide4(d4);">Mounts</a>
        </li>
        <li><a href="#" onclick="showhide5(d5);">Accessories</a>
        </li>
    </ul>
</div>
<div id="d1" class="dropContent" style="display:none;">
This is Content 1.
</div>
<div id="d2" class="dropContent" style="display:none;">
This is Content 2.
</div>
<div id="d3" class="dropContent" style="display:none;">
This is Content 3.
</div>
<div id="d4" class="dropContent" style="display:none;">
This is Content 4.
</div>
<div id="d5" class="dropContent" style="display:none;">
This is Content 5.
</div>

Javascript:

<script type="text/javascript">
    function hide(){
        d1.style.display = 'none', 
        d2.style.display = 'none',
        d3.style.display = 'none', 
        d4.style.display = 'none', 
        d5.style.display = 'none'; 
    }

    function showhide1() {
        document.getElementById(d1);
        if(d1.style.display == 'block')
            hide();
        else
            d1.style.display = 'block', 
            d2.style.display = 'none',
            d3.style.display = 'none', 
            d4.style.display = 'none', 
            d5.style.display = 'none'; 
    }
    function showhide2() {
        document.getElementById(d2);
        if(d2.style.display == 'block')
            hide();
        else
            d2.style.display = 'block', 
            d1.style.display = 'none', 
            d3.style.display = 'none', 
            d4.style.display = 'none', 
            d5.style.display = 'none'; 
    }
    function showhide3() {
        document.getElementById(d3);
        if(d3.style.display == 'block')
            hide();
        else
            d1.style.display = 'none', 
            d2.style.display = 'none',
            d3.style.display = 'block', 
            d4.style.display = 'none', 
            d5.style.display = 'none'; 
    }
    function showhide4() {
        document.getElementById(d4);
        if(d4.style.display == 'block')
            hide();
        else
            d1.style.display = 'none', 
            d2.style.display = 'none',
            d3.style.display = 'none', 
            d4.style.display = 'block', 
            d5.style.display = 'none'; 
    }
    function showhide5() {
        document.getElementById(d5);
        if(d5.style.display == 'block')
            hide();
        else
            d1.style.display = 'none', 
            d2.style.display = 'none',
            d3.style.display = 'none', 
            d4.style.display = 'none', 
            d5.style.display = 'block'; 
    }

</script>

Java脚本

function showhide(obj) {
                document.getElementById('d1').style.display = 'none';
                document.getElementById('d2').style.display = 'none';
                document.getElementById('d3').style.display = 'none';
                document.getElementById('d4').style.display = 'none';
                document.getElementById('d5').style.display = 'none';
                document.getElementById(obj).style.display = 'block';
            }

Html

<div id="dropMenu">
    <ul>
        <li><a href="#" onclick="showhide('d1');">Advanced AGM</a>
        </li>
        <li><a href="#" onclick="showhide('d2');">Lithium-ION</a>
        </li>
        <li><a href="#" onclick="showhide('d3');">Chargers</a>
        </li>
        <li><a href="#" onclick="showhide('d4');">Mounts</a>
        </li>
        <li><a href="#" onclick="showhide('d5');">Accessories</a>
        </li>
    </ul>
</div>
<div id="d1" class="dropContent" style="display:block;">
This is Content 1.
</div>
<div id="d2" class="dropContent" style="display:none;">
This is Content 2.
</div>
<div id="d3" class="dropContent" style="display:none;">
This is Content 3.
</div>
<div id="d4" class="dropContent" style="display:none;">
This is Content 4.
</div>
<div id="d5" class="dropContent" style="display:none;">
This is Content 5.
</div>

希望它能帮助