[英]How to match a part of a string with array values using jquery / javascript
I have a javascript array: 我有一个javascript数组:
var exclude = ["Santorum","Obama","Romney","Gingrich"];
I have html links: 我有html链接:
<a href="" class="title">Santorum is seeking campaign funding</a>
<a href="" class="title">Clinton is stepping down as Secretary</a>
<a href="" class="title">Obama is seeking reelection</a>
I want to check whether any of the a-links have any one of the exclude-values in them, and if they do, then remove them. 我想检查是否任何a链接中都有任何排除值,如果有,则将其删除。 So the result would be:
因此结果将是:
<a href="" class="title">Clinton is stepping down as Secretary</a>
The other two would be removed as they possess words from the array. 其他两个将被删除,因为它们拥有数组中的单词。 I've tried using jQuery.inArray but I can't seem to figure it out.
我尝试使用jQuery.inArray,但似乎无法弄清楚。 Thx for your help.
谢谢您的帮助。
Something like: 就像是:
var exclude = ["Santorum","Obama","Romney","Gingrich"];
$('a.title').filter($.map(exclude, function (val) {
return ':contains("' + val + '")';
}).join()).remove();
Try looping though the excluded words, and remove links that have them. 尝试遍历排除的单词,并删除包含它们的链接。
For example (using the :contains
selector ): 例如(使用
:contains
选择器 ):
$.each(exclude, function(i, v){
$('a.title:contains("'+v+'")').remove();
});
DEMO: http://jsfiddle.net/H6FPb/ 演示: http : //jsfiddle.net/H6FPb/
EDIT: You said the <a>
is wrapped in a <div>
, then you can do: 编辑:您说
<a>
包装在<div>
,那么您可以执行以下操作:
$.each(exclude, function(i, v){
$('a.title:contains("'+v+'")').parent('div').remove();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.