获得jQuery .filter()结果的反向/反向
我有一个.filter
函数,它可以工作并标记
但是,我需要恰恰相反。 因此,返回所有不包含<input
字段。 我试过if ($(this).html().indexOf('<input') == -1) {
但这只是以某种方式标记所有行。
我的jQuery:
$(document).ready(function () { $("#ListView1_itemPlaceholderContainer table table table td").filter(function (index) { if ($(this).html().indexOf(' 0) { return true; } }).closest('div').css('background-color', 'red'); });
这是我的HTML
dfgdfg @dfgdfg
Follower-4578 , Following-1654
dfgdfgd @dfgdfgdf
Follower-4622 , Following-4007
我也查看了这篇文章jQuery:使用filter(),但是按照评论者的建议使用这两个结果 。
并尝试了这个:
$(document).ready(function () { $.fn.invert = function () { return this.end().not(this); }; $("#ListView1_itemPlaceholderContainer table table table td").filter(function (index) { if ($(this).html().indexOf(' 0) { return true; } }).invert().closest('div').css('background-color', 'red'); });
但这也使所有行变红。
您可以使用.not()以及:has()
$(document).ready(function () { $("#ListView1_itemPlaceholderContainer table table table td").not(':has(input)').closest('div').css('background-color', 'red'); });
我想你应该接受阿伦的回答。 问题是indexOf返回第一个匹配字符的索引http://www.w3schools.com/jsref/jsref_indexof.asp ,在您的情况下它将为0,因此条件> 0将不适用。 同样关于== -1关注所有行的问题,最好将你的函数应用于tr而不是td
您可以将function更改为
$(document).ready(function () { $("#ListView1_itemPlaceholderContainer table table table tr").filter(function (index) { if ($(this).html().indexOf(' -1) { return true; } }).closest('div').css('background-color', 'red'); });
用输入红色标记行,或反转条件做相反的操作。 或者更好的是使用Arun建议的不同选择器。
用输入 http://jsfiddle.net/q9othg59/2/标记红色行的小提琴,并为相反的http://jsfiddle.net/q9othg59/4/小提琴