简体   繁体   English

如果div没有多个类别之一,则将其隐藏

[英]Hide a div if it does not have one of several classes

I have a list of links: 我有一个链接列表:

<ul>
   <li><a class="selection" href="appels">Appels</a></li>
   <li><a class="selection" href="bananas">Bananas</a></li>
   <li><a class="selection" href="pineapples">Pineapples</a></li>
</ul>

Beneath it I have several boxes with the general term "fruits" and one or more specific fruits as its classes: 在它下面,我有几个带有通用术语“水果”的盒子,以及一个或多个特定水果作为其类别:

<div class="fruits bananas apples pineapples"></div>  
<div class="fruits bananas"></div>
<div class="fruits pineapples apples"></div>

My goal: When I click on a link, then all the boxes which do not have the selected fruit contained in their class-attribute shall hide. 我的目标:当我单击一个链接时,所有隐藏在其类别属性中包含所选水果的框将被隐藏。 When the document loads, by default all div-boxes will be displayed. 加载文档时,默认情况下将显示所有div框。

Here is the jQuery I have come up with so far: 这是到目前为止我提出的jQuery:

jQuery(document).ready(function() {

    var element_just_clicked;
    var href_of_element;

    jQuery('.selection').click(function() { 

                event.preventDefault();                             
                element_just_clicked = jQuery(this);                    
                href_of_element = element_just_clicked.attr('href');


                // ... and now???
    });

Firstly, you have to prevent the default action of those anchors, as they will redirect. 首先,您必须阻止这些锚点的默认操作,因为它们将被重定向。
Then, you have to get the href attribute ( not the property), and use that for the classname, and then it's just filtering and showing and hiding. 然后,您必须获取href属性( 而不是属性),并将其用作类名,然后才进行过滤,显示和隐藏。

jQuery(function($) {
    $('.selection').on('click', function(e) {
        e.preventDefault();
        var elems = $('.' + this.getAttribute('href')).show();
        $('.fruits').not(elems).hide();
    });
});

FIDDLE 小提琴

One possible approach: 一种可能的方法:

$('.selection').click(function(e) {
  $('.fruits').show().not('.' + this.getAttribute('href')).hide();
  e.preventDefault();
})

Demo . 演示 One important note here: you have to prevent links to be processed as, well, links - hence 这里的一个重要说明:您必须防止链接像链接一样被处理-因此 return false (kudos to @cookiemonster for reminding me about event propagation) e.preventDefault() call. (向@cookiemonster鸣谢,提醒我有关事件传播) e.preventDefault()调用。

I'd suggest using some data attribute to record this relationship - in my practice, data-target is often the suitable name. 我建议使用一些data属性来记录这种关系-在我的实践中, data-target通常是合适的名称。 For example: 例如:

<ul>
   <li><a href="javascript:void(0);" data-target="apples">Appels</a></li>
   <li><a href="javascript:void(0);" data-target="bananas">Bananas</a></li>
   <li><a href="javascript:void(0);" data-target="pineapples">Pineapples</a></li>
</ul>

JS: JS:

$('a[data-target]').click(function() {
  $('.fruits').show().not('.' + this.dataset.target).hide();
});

Demo . 演示 One has to swap this.dataset.target for this.getAttribute('data-target') when dealing with browsers that don't support Dataset API - unfortunately, all IE but IE11 fall into this category. 在处理不支持数据集API的浏览器时,必须将this.dataset.target交换为this.getAttribute('data-target') -不幸的是,除IE11之外的所有IE都属于这一类。

$(".selection").click(function() { 
    var href = $(this).attr("href");
    $("div.fruits").hide().filter("." + href).show();
    return false;
});

fiddle: http://jsfiddle.net/uyLeZ/ 小提琴: http//jsfiddle.net/uyLeZ/

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

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