简体   繁体   English

如果匹配列表,jQuery隐藏元素,无论顺序如何

[英]jQuery hide elements if matched to list regardless of order

I need to hide span elements if they match words in a wordList. 如果它们匹配wordList中的单词,我需要隐藏span元素。

HTML: HTML:

<span class="wordBank_Words">
     <span word="hello"></span>
     <span word="you"></span>
     <span word="hi"></span>
</span>        

JavaScript: JavaScript的:

wordList = ['hello', 'how', 'are', 'you'];

$('.wordBank_Words span').each(function (index, val) {
    if ($(this).attr('word').match(wordList)) {
        $(this).hide();
    }
});

So, if done correctly, it should hide 'hello' and 'you', but not 'hi' 所以,如果做得正确,它应该隐藏'你好'和'你',但不是'你好'

If I do match('hello') , this correctly hides 'hello' span from the HTML element list. 如果我match('hello') ,这会正确隐藏HTML元素列表中的'hello'范围。

But I need to loop through each span element in wordBank_Words , and compare them against each word in the wordList, and only hide if there is a match. 但是我需要循环遍历wordBank_Words每个span元素,并将它们与wordList中的每个单词进行比较,并且只有在匹配时才隐藏。 They need to be compared regardless of order. 无论顺序如何,都需要进行比较。

How can this be done? 如何才能做到这一点?

No need of looping over all the elements. 无需循环遍历所有元素。

You can create an attribute-value selector from array by joining them to form the correct selector. 您可以通过连接它们以形成正确的选择器,从数组创建属性值选择器。

'[word="' + wordList.join('"], [word="') + '"]'

will create a selector as 将创建一个选择器作为

[word="hello"], [word="how"], [word="are"], [word="you"]

which then can be passed to jQuery. 然后可以传递给jQuery。

Demo 演示

 var wordList = ['hello', 'how', 'are', 'you']; var selector = '[word="' + wordList.join('"], [word="') + '"]'; $('.wordBank_Words').find(selector).hide(); 
 span { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <span class="wordBank_Words"> <span word="hello">Hello</span> <span word="you">You</span> <span word="hi">Hi</span> <span word="not">Not Found</span> </span> 


You can also use attr / prop methods as follow 您还可以使用attr / prop方法,如下所示

 var wordList = ["hello", "how", "are", "you"], re = wordList.join("|"); $(".wordBank_Words span").attr('word', function(index, val) { if (wordList.indexOf(val) > -1) $(this).hide(); return val; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <span class="wordBank_Words"> <span word="hello">hello</span> <span word="you">you</span> <span word="hi">hi</span> </span> 

I would change the word-attributes to html 5 valid data-attributes: 我会将word-attributes更改为html 5有效数据属性:

<span class="wordBank_Words">
     <span data-word="hello"></span>
     <span data-word="you"></span>
     <span data-word="hi"></span>
</span>

Then loop through the spans and hide the ones on the list: 然后遍历跨度并隐藏列表中的那些:

$(function () {
    var wordList = ['hello', 'how', 'are', 'you'];
    $(".wordBank_Words span").each(function () {
        if (wordList.indexOf($(this).data("word")) > -1) {
            $(this).hide();
        }
    });
});

You can use Array.indexOf() to check whether the word exists in the array like 您可以使用Array.indexOf()来检查数组中是否存在单词

 var wordList = ['hello', 'how', 'are', 'you']; $('.wordBank_Words span').each(function(index, val) { $(this).toggle(wordList.indexOf($(this).attr('word')) == -1) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="wordBank_Words"> <span word="hello"></span> <span word="you"></span> <span word="hi"></span> </span> 

Try adding | 尝试添加| to RegExp using .join() , !== null after .match() to condition .match()之后使用.join()!== null .match() RegExp

 var wordList = ["hello", "how", "are", "you"], re = wordList.join("|"); $(".wordBank_Words span").each(function(index, val) { if ( $(this).attr('word').match(re) !== null ) { $(this).hide(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <span class="wordBank_Words"> <span word="hello">hello</span> <span word="you">you</span> <span word="hi">hi</span> </span> 

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

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