[英]How to check if data-attribute does not contain a string with :contains in jQuery
Is it possible to apply :not(:contains())
selector to data-attribute? 是否可以将
:not(:contains())
选择器应用于数据属性? Here is what I have in HTML: 这是我在HTML中的功能:
<input id="keyword">
<button id="find1">Find - 1</button>
<button id="find2">Find - 2</button>
<div class="list_item" data-stringtofind="abc def">abc def</div>
<div class="list_item" data-stringtofind="ghi jkl">ghi jkl</div>
<div class="list_item" data-stringtofind="mno prs">mno prs</div>
What I'd like to do is to filter it on click based on what is inside of data-stringtofind
attribute. 我想做的是根据
data-stringtofind
属性内的内容在点击时对其进行过滤。 I'm trying with this code, but it's not working. 我正在尝试使用此代码,但无法正常工作。
$('#find1').on('click', function () {
var findthis = $('#keyword').val();
console.log(findthis);
$( ".list_item.data('stringtofind'):not(:contains("+ findthis +"))").hide();
console.log(".list_item.data('stringtofind')");
});
Here is what the console outputs: 这是控制台输出的内容:
Error: Syntax error, unrecognized expression: .list_item.data('stringtofind'):not(:contains(abc))
Am I doing some simple mistake or this just isn't possible to do with :not:contains? 我是在做一些简单的错误,还是:not:contains无法做到?
This code works, but it searches inside of the div, not inside of the data-stringtofind attribute. 该代码有效,但是它在div内而不是在data-stringtofind属性内进行搜索。
$('#find2').on('click', function () {
var findthis = $('#keyword').val();
console.log(findthis);
$( ".list_item:not(:contains("+ findthis +"))").hide();
console.log(".list_item.data('stringtofind')");
});
Here is a fiddle: https://jsfiddle.net/mk7yr62k/2/ 这是一个小提琴: https : //jsfiddle.net/mk7yr62k/2/
:contains
isn't related to attributes at all. :contains
与属性完全无关。 You want an attribute substring selector: 您需要一个属性子字符串选择器:
$( ".list_item:not([data-stringtofind*='" + findthis + "'])").hide();
[data-stringtofind*='xxx']
means " xxx
anywhere in the data-stringtofind
attribute." [data-stringtofind*='xxx']
的意思是“ xxx
在任何地方data-stringtofind
属性”。 And of course, :not
negates it. 当然,
:not
否定它。
Here's an example: 这是一个例子:
var p = $("<p>").text("Waiting briefly...").appendTo(document.body); setTimeout(function() { var findthis = "abc"; $( ".list_item:not([data-stringtofind*='" + findthis + "'])").hide(); p.text("Hid the ones not matching 'abc'"); }, 500);
<div class="list_item" data-stringtofind="abc def">abc def</div> <div class="list_item" data-stringtofind="ghi jkl">ghi jkl</div> <div class="list_item" data-stringtofind="mno prs">mno prs</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
:contains
is used to find an element by the text it contains, not the contents of an attribute, and hence is not suitable for what you require. :contains
用于通过元素包含的文本而不是属性的内容来查找元素,因此不适合您的需要。
An alternative would be the attribute contains selector, but this will match on spaces too - ie. 一种替代方法是该属性包含选择器,但这也将在空格上匹配-即。
bc de
would hit your first element. bc de
将打您的第一个元素。
To fix this you could use filter()
and convert the data attribute of each element to an array and use indexOf()
to find a match. 要解决此问题,您可以使用
filter()
并将每个元素的数据属性转换为数组,然后使用indexOf()
查找匹配项。 Try this: 尝试这个:
$('#find1').on('click', function () {
var findthis = $('#keyword').val();
$('.list_item').filter(function() {
var arr = $(this).data('stringtofind').split(' ');
return arr.indexOf(findthis) != -1;
});
});
Try this, it searches for your string in each .list_item
and hides it if there's no match. 尝试此操作,它会在每个
.list_item
搜索您的字符串,如果不匹配.list_item
其隐藏。
$('#find1').on('click', function () {
var findthis = $('#keyword').val();
$(".list_item").each(function() {
if ($(this).data('stringtofind').indexOf(findthis) == -1)
$(this).hide();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.