简体   繁体   English

单击列表元素即可显示和隐藏div

[英]Show and hide divs on click of a list element

I have this list: 我有这个清单:

<ul class="filters">
      <li id="all">All</li>
      <li id="cat1">Cat1</li>
      <li id="cat2">cat2</li>
      <li id="cat3">cat3</li>
</ul>

<div class="col s12 m4 l3 category-cat1">
     <div class="card-panel grey lighten-5 z-depth-1">
       <div class="row">
          <div class="col s4">
            <img src="/static/images/cat124.jpg" alt="" class="circle responsive-img">
          </div>
          <div class="col s8">
             <span class="black-text">
                This is a square image. Add the "circle" class to it to make it appear circular.
             </span>
          </div>
        </div>
        <a href="/">View Details</a>
      </div>
</div>
....
....

And this JS code on the end of the page: 而此JS代码在页面末尾:

$('.filters li').click(function () {
        $('[class^=category-]').hide('fast','linear');
        var id = $(this).attr('id');
        if(id == 'all') {
             $('[class^=category-]').show('fast','linear');
        } else {
             $('.category-'+id).show('fast','linear');
        }
    });

I try to show and hide divs with the same category through the filters. 我尝试通过过滤器显示和隐藏具有相同类别的div。 I thought that it would be easy, but when I click on the <li> nothing happens. 我以为这很容易,但是当我单击<li>什么也没有发生。 Neither an error on the console, nor a change on the divs. 控制台上没有错误,也没有div上的更改。

The problem is the selector ^= , which is the attribute starts with selector , your class name attribute value does not starts with category- that is why it is not working. 问题是选择器^= ,这是属性以选择器开头 ,您的类名属性值不是以category-开头,这就是为什么它不起作用的原因。

Instead try attribute contains selector 相反,try属性包含选择器

$('[class*=category-]').hide('fast','linear');

But the right solution could be to add one more class to those div elements like category 但是正确的解决方案可能是向这些div元素(如category添加一个category

<div class="col s12 m4 l3 category-cat1 category">
</div>

then use it 然后用它

$('div.category').hide('fast','linear');

You are using the wrong attribute selector. 您使用了错误的属性选择器。

The ^ means begins with and your class attribute doesn't. ^的意思是开头,而您的class属性则不是。 Using * means 'Contains a substring'. 使用*表示“包含子字符串”。

The second problem that you have is your id comparison uses only 2 == which is JavaScript is a weak comparison. 您遇到的第二个问题是ID比较仅使用2 ==,这是JavaScript的比较弱。 Change this to 3 === and things will start to work. 将此更改为3 ===,一切将开始工作。

See this JSFiddle: http://jsfiddle.net/bey3wpt2/ 看到这个JSFiddle: http : //jsfiddle.net/bey3wpt2/

Also see below. 另请参阅下文。

$('.filters li').click(function () {
    var id = $(this).attr('id');

    $('[class*=category-]').hide('fast', 'linear');

    if (id === 'all') {
        $('[class*=category-]').show('fast', 'linear');
    } else {
        $('.category-' + id).show('fast', 'linear');
    }
});

Ps The only reason I moved to the var id up to the top is to satisfy JSLint. Ps我移到顶部的var id的唯一原因是为了满足JSLint。

Here, the problem is given selector format [attribute^='startingString'] will return the element which attribute start with given string. 在这里,问题是给定选择器格式[attribute ^ ='startingString']将返回属性以给定字符串开头的元素。

<div class="col s12 m4 l3 category-cat1"> // here, class start with "col" so no element will be select. So selector is need to change like below format.




  $("[class^='col']").hide('fast', 'linear');

//selector start with "col"

or use * for, all elements with a class attribute value containing the word "category-" 或对所有具有类属性值包含单词“ category-”的元素使用*

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

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