Jquery .filter()问题

3

给定以下HTML:

<div class="parent">
<span class="child">10</span>
</div>

<div class="parent">
<span class="child">20</span>
</div>

我想根据其子元素的值(即子值)过滤和隐藏父div,因此考虑进行以下操作:
<script>
$('span.child').filter(function(index) {
 return ($(this).text() < '15');
}).$(this).parent().hide();
</script>

没有成功...我无法根据Span值隐藏父级Div...
有人能帮我吗?
非常感谢。
2个回答

2

试试这个:

jQuery('span.child').filter(function(index) {
   return (jQuery(this).text() < '15');  
}).parent().hide();

2

只需删除最后的$(this),像这样:

$('span.child').filter(function(index) {
  return $(this).text() < '15';
}).parent().hide();

.filter() 返回经过筛选后的元素集合,它会将您选择的内容进行筛选,过滤掉您想要去除的部分,然后您只需继续链接结果即可。 您可以在此处查看其工作原理


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接