本文介绍了JavaScript的切换显示/隐藏分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript切换功能:

I have a javascript toggle function:

<script type="text/javascript">
    function toggle(layer) {
    var d = document.getElementById(layer);
    d.style.display = (d.style.display == 'none') ? '' : 'none';
    }
    </script>

这样做是:

我有页上和这些链接的点击几个环节就显示/隐藏与之相关的各自的DIV部分。

I have a few links on page and on click of these links it shows / hides the respective DIV section associated with it..

在以下两个链接它打开和关闭的div部分名为stusearch&安培; facsearch

In the following two links it opens and closes div section named stusearch & facsearch

<a href="javascript:toggle('stusearch')" ><b>Student Manager</b></a>
<a href="javascript:toggle('facsearch')" ><b>Faculty Manager</b></a>

这工作得很好,只是,我想隐藏点击一个新的肘杆时切换显示的previous,在previous任意一方残留打开的那一刻,而新的下方打开。

This works well except that, i would like to hide the previous shown toggle when a new toggle link is clicked, at the moment the previous one remains open, and the new one opens up below it.

推荐答案

我调整你的codeA位。我最终加入了变量来存储你想显示的情况下,要增加更多的div切换/隐藏的div:

I tweaked your code a bit here. I ended up adding a variable to store the divs you want to show/hide in case you want to add more divs to toggle:

var divs = [ "stusearch", "facsearch" ];
function toggle(layer) {
    var d
    for(var i = 0; i < divs.length; i += 1) {
        d = document.getElementById(divs[i]);
        d.style.display = 'none';
    }
    d = document.getElementById(layer);
    d.style.display = '';
}

这篇关于JavaScript的切换显示/隐藏分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:37