简单的 Onclick Javascript 下拉导航,无需使用开关/选项卡

发布于 2025-01-03 07:54:43 字数 3417 浏览 3 评论 0原文

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

Pretty new to using Javascript. Would like to avoid using Jquery or other frameworks.

This is a simple drop down navigation I'm trying to create using a script I found here:
http://blog.movalog.com/a/javascript-toggle-visibility/

I could use some help cleaning up some code, and for some pointers. It works the way I need it, but would like to shorten it up a bit. Thanks.

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2025-01-10 07:54:43

Java Script

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>

希望有帮助


Java Script

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>

Hope it help


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文