开发者

Jquery .filter() Question

开发者 https://www.devze.com 2022-12-25 21:53 出处:网络
Given the following HTML: <div class=\"parent\"> <span class=\"child\">10</span> </div>

Given the following HTML:

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

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

I want to filter and hide parent div based on it's span value (child value), so have thought of doing something like:

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

No success... I am not hiding Parent div based on Span Value..

Anyone can help me ?

Greatly apprec开发者_如何学Goiated.


Try this:

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


Just remove that last $(this), like this:

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

.filter() returns the filtered set of elements, it takes what you selected, filters out what you wanted gone, then you just keep chaining the result. You can see this working here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号