简体   繁体   English

JavaScript - 隐藏所有其他 div

[英]JavaScript - Hide all other divs

I've a number of divs that are only visible after clicking a link.我有许多 div 仅在单击链接后才可见。 How to close all open divs so that only the clicked one is visible?如何关闭所有打开的 div 以便只有点击的那个可见?
I'm using this .js:我正在使用这个 .js:

    function showhide(id){
        if (document.getElementById) {
        var divid = document.getElementById(id);
        divid.style.display = (divid.style.display=='block'?'none':'block');
        } }

        <a href="javascript:void(null)" onclick="showhide('features');">features</a>
<a href="javascript:void(null)" onclick="showhide('design');">design</a>
<a href="javascript:void(null)" onclick="showhide('create');">create</a>

Thanks Uli谢谢乌利

One way is to add a class and seek the elements based on that to hide them ( as shown in other answers )一种方法是添加一个类并基于该类查找元素以隐藏它们(如其他答案中所示

An alternative is to store the state of the elements in an object and use that to identify the open ones that need closing..另一种方法是将元素的状态存储在一个对象中,并使用它来识别需要关闭的打开的元素。

var divState = {}; // we store the status in this object
function showhide(id) {
    if (document.getElementById) {
        var divid = document.getElementById(id);

        divState[id] = (divState[id]) ? false : true; // initialize / invert status (true is visible and false is closed)
        //close others
        for (var div in divState){
            if (divState[div] && div != id){ // ignore closed ones and the current
                document.getElementById(div).style.display = 'none'; // hide
                divState[div] = false; // reset status
            }
        }
        divid.style.display = (divid.style.display == 'block' ? 'none' : 'block');
    }
}

Demo at http://jsfiddle.net/gaby/LfzYc/演示在http://jsfiddle.net/gaby/LfzYc/

You can use document.getElementByTagName to retrieve all divs.您可以使用document.getElementByTagName来检索所有 div。

Then iterate over them and for each one set the style to block if it's the div obtained from getElementById before, or none otherwise.然后迭代它们,如果是之前从getElementById获得的 div,则为每个将样式设置为block ,否则none

(I suggest to add a class to all the divs relevant here, and only consider the divs with this class during the iteration, so that unrelevant parts of the page won't be affected.) (我建议这里所有相关的div都加一个类,迭代时只考虑这个类的div,不影响页面不相关的部分。)

The following code will hide all div and show the one you click on...以下代码将隐藏所有 div 并显示您单击的那个...

function showhide(id){
        if (document.getElementById) {
          var divid = document.getElementById(id);
          var divs = document.getElementsByClassName("hideable");
          for(var div in divs) {
             div.style.display = "none";
          }
          divid.style.display = "block";
        } 
        return false;
 }

<a href="#" onclick="showhide('features');" >features</a>
<a href="#" onclick="showhide('design');"  >design</a>
<a href="#" onclick="showhide('create');" >create</a>

<div class="hideable" id="features">Div 1</div>
<div class="hideable" id="design">Div 2</div>
<div class="hideable" id="create">Div 3</div>

I also added the return false;我还添加了return false; to preven the link's default behavior.防止链接的默认行为。 It's cleaner and easier to understand than using javascript:void(null) on the link's href attribute.它比在链接的href属性上使用javascript:void(null)更清晰、更容易理解。 Does it solve your issue?它解决了你的问题吗?

The IDs or Class name of the div could be updated on a temporary array and a simple if condition would help.可以在临时数组上更新 div 的 ID 或类名称,简单的 if 条件会有所帮助。 A similar snippet that worked for me in my usecase.在我的用例中对我有用的类似片段。

var screens = ["screen_splash","screen_login","screen_register","route_getotp","route_loginvalidate","route_inputotp","screen_dashboard","screen_search","screen_cart","screen_myaccount"];

function changescreen(screen)
{
    screens.forEach(function(value)
    {
        if(value == screen){
            $("#"+value).show();
        }else{
            console.log("Disable Screen : " +value);
            $("#"+value).hide();
        }
    });

}

Note: Remember to change # to .注意:记得把 # 改成 . based on the usage of id or class name on your code.基于代码中 id 或类名的使用。

Using a classname is the typical way to do this but I'd recommend to use a lib like jquery/rightjs/whateveryoulike to do this;使用类名是执行此操作的典型方法,但我建议使用像 jquery/rightjs/whateveryoulike 这样的库来执行此操作; because: getElementsByClassName() is not supported by < IE9 and querySelectorAll() not supported by < IE8.因为:< IE9 不支持 getElementsByClassName(),< IE8 不支持 querySelectorAll()。 If you can not use a lib, then you would need to iterate over all divs via getElementsByTagName("div") and check for that class.如果您不能使用 lib,则需要通过 getElementsByTagName("div") 遍历所有 div 并检查该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM