简体   繁体   English

如何基于关键字过滤器切换div的可见性

[英]How to toggle visibility of divs based on keyword filters

I have this HTML of some cards that contain different keywords: 我有一些包含不同关键字的卡片的HTML:

<div class="item">
    <div class="hashtags">
        <span><a href="#">Healthcare</a></span>
        <span><a href="#">Mobility</a></span>
    </div>
</div>
<div class="item">
    <div class="hashtags">
        <span><a href="#">Finance</a></span>
        <span><a href="#">Mobility</a></span>
    </div>
</div>

...and this HTML of some pills that can be toggled on/off: ...以及一些可以打开/关闭的药丸的HTML:

<div class="hashtags">
    <span class="active"><a href="#">Finance</a></span>
    <span class="inactive"><a href="#">Healthcare</a></span>
    <span class="inactive"><a href="#">Managed Services</a></span>
    <span class="inactive"><a href="#">Mobility</a></span>
</div>

The idea is to be able to click on a pill for "Finance", and only those items that contain "Finance" in their hashtag class will be displayed, and all other items are hidden. 这个想法是能够单击“ Finance”的药丸,并且只会显示其标签类中包含“ Finance”的那些项目,而所有其他项目都将被隐藏。 I've tried searching for how to filter/toggle based on content, but can't seem to find anything yet. 我尝试搜索如何根据内容进行过滤/切换,但似乎找不到任何内容。 Here's my JS so far, which only toggles the active/inactive classes of the pill styling: 到目前为止,这是我的JS,它仅切换药丸样式的有效/无效类:

    $('.hashtags>span').click(function() {
    $(this).toggleClass('inactive');
    $(this).toggleClass('active');
    });

I know there's a way to lookup HTML and compare strings in JS or jQuery...but not sure that's the best approach. 我知道有一种方法可以查找HTML并比较JS或jQuery中的字符串...但不确定那是最好的方法。 Any advice is appreciated. 任何建议表示赞赏。 Thanks very much! 非常感谢!

Here you are a complete example. 这里是一个完整的示例。 I add comments to code 我在代码中添加注释

 $('.hashtags > span').on('click', function(e) { e.preventDefault(); // first we store value var storedValue = $(this).text(); // send it to a function highlightHashtag(storedValue); }); var highlightHashtag = function(value) { // iterate through all $('.hashtags > span').each(function() { if($(this).text() !== value) { // if doesnt match, inactive $(this).removeClass('active'); $(this).addClass('inactive'); } else { $(this).removeClass('inactive'); $(this).addClass('active'); } }); }; 
 .active { background-color:green; } .inactive { background-color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="item"> <div class="hashtags"> <span><a href="#">Healthcare</a></span> <span><a href="#">Mobility</a></span> </div> </div> <div class="item"> <div class="hashtags"> <span><a href="#">Finance</a></span> <span><a href="#">Mobility</a></span> </div> </div> 

i think i understand you question, first of all, hide all .items, then show all .items with a hashtag containing the text you are selected. 我想我理解您的问题,首先,隐藏所有.items,然后显示所有带有包含您所选文本的标签的.items。 like this (untested) 像这样(未经测试)

FINAL SOLUTION 最终解决方案

$('.hashtags>span').unbind('click').click(function() {
    var text = $(this)
            .toggleClass('inactive')
            .toggleClass('active');
    var items = $('.item').hide();
    $('.hashtags>span.active').each(function(){
        var t = $(this).find('a:first').text() ;
        items.each(function(){
            var i = $(this);
            //find all items containing a a with the activated hashtag text
            if(i.find('.hashtags>span>a:contains(\'' + t + '\')').length > 0) {
                i.show();
            }
        });
    });
});

In my opinion is better if you add some custom tag into you html. 我认为如果您在HTML中添加一些自定义标签,效果会更好。 Look at my quick example: 看我的简单例子:

In the html you can add some custom attributes like data-something that can be really usefull. 在html中,您可以添加一些自定义属性,例如data-something,它们可能确实有用。

CSS only highlight which class is on which object CSS仅突出显示哪个类位于哪个对象上

You can try my code down here or look at it on JSFiddle https://jsfiddle.net/w19ytk8p/ 您可以在此处尝试我的代码,或在JSFiddle上查看它:https: //jsfiddle.net/w19ytk8p/

 $('.hashtags>span').click(function() { $(this).toggleClass('inactive'); $(this).toggleClass('active'); var tag = $(this).attr("data-tag"); var related = []; $('.hashtags').each(function(index){ $(this).children( "span" ).removeClass("active"); $(this).children( "span" ).addClass("inactive"); if($(this).attr("data-tag") !== undefined && $(this).attr("data-tag").indexOf(tag)>-1) {related.push(this);} }); $(related).each(function(){ $(this).children( "span" ).removeClass("inactive"); $(this).children( "span" ).addClass("active"); }); }); 
 .inactive > a{ color: red; } .active > a{ color: blue; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="item"> <div class="hashtags" data-tag="healthcare finance"> <span><a href="#">Healthcare</a></span> <span><a href="#">Mobility</a></span> </div> </div> <div class="item"> <div class="hashtags" data-tag="finance mobility"> <span><a href="#">Finance</a></span> <span><a href="#">Mobility</a></span> </div> </div> <div class="hashtags"> <span class="active" data-tag="finance"><a href="#">Finance</a></span> <span class="inactive" data-tag="healthcare"><a href="#">Healthcare</a></span> <span class="inactive" data-tag="managedServices"><a href="#">Managed Services</a></span> <span class="inactive" data-tag="mobility"><a href="#">Mobility</a></span> </div> 

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

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