简体   繁体   English

使所有其他Divs消失javascript

[英]Make all other Divs dissapear javascript

I got a Script Which makes me onclick showing all Divs with Specific id 我有一个脚本,可以让我onclick显示具有特定ID的所有Divs

This is the Selector 这是选择器

<a onclick="filterGal('simpleCart_shelfItem item Sonnenbrillen')" href="javascript:void(0);">Sonnenbrillen</a>

this is my Script 这是我的剧本

function filterGal(foo) {
                    var toHide = document.getElementsByClassName(foo);
                    for (i = 0; i < toHide.length; i++) {
                            toHide[i].style.display = 'block';
                    }
            }

So now my question how can i only show specific div with classname and display none the other div with other classnames? 因此,现在我的问题是如何只显示具有类名的特定div而不显示具有其他类名的其他div?

Try this : 尝试这个 :

function filterGal(foo) {
    var toHide = document.getElementsByTagName('div');
    for (i = 0; i < toHide.length; i++) {
         toHide[i].style.display = 'none';
    }
    var toShow = document.getElementsByClassName(foo);
    for (i = 0; i < toShow.length; i++) {
         toShow[i].style.display = 'block';
    }
}

Can be improved by skiping in the first loop div which match your classes. 可以通过跳过与您的班级相匹配的第一个循环div进行改进。

I just show you add jquery tag so you just need to do this : 我只是告诉您添加jquery标签,所以您只需要这样做:

function filterGal(foo) {
    $( "div:not("+foo+")" ).hide();
}

Exemple : http://jsfiddle.net/ACjeZ/1 范例: http//jsfiddle.net/ACjeZ/1

This is what I would try: 这是我会尝试的:

$("div").Hide();
$("." + foo).Show();

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

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