jquery过滤内部div的内容

时间:2016-04-06 08:32:50

标签: jquery

现在我有一个功能可以根据内部内容过滤div。但是,目前它基于主div容器内的所有内容进行过滤(在此示例中为div class="name")。但是,我希望它通过div中的div进行过滤(在此示例中为div class="addresss")。

我想在下面的代码中完成我想要做的事情。

$(document).on('click', '#run', function(){
var comment = "Street Place";    
var searching = $('.name').children('.address:contains('+comment.value+')');  
searching.filter(':contains('+comment.value+')').show();    
});

对于此示例,我希望我的Run函数根据其与var comment的兼容性显示整个div,这恰好与div address.具有相同的信息

JsFiddle Link https://jsfiddle.net/4xe28x5z/

3 个答案:

答案 0 :(得分:2)

您可以使用IEnumerable<int> numbers = Enumerable.Range(0, 10); IEnumerable<int> evenNumbers = numbers .ToObservable() .SelectMany(async i => new { Value = i, IsMatch = await IsEven(i) }) .Where(a => a.IsMatch) .Select(a => a.Value) .ToEnumerable(); async Task<bool> IsEven(int i) { await Task.Delay(100); return i % 2 == 0; } 过滤器与:has()根据:contains()过滤name

&#13;
&#13;
address
&#13;
$(document).on('click', '#run', function() {
  var comment = "Street Place";
  var searching = $('.name').has('.address:contains("' + comment + '")');
  searching.show();
});
&#13;
.name {
  display: none;
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

Below is the modified code

$(document).on('click', '#run', function(){
var comment = "Street Place";    
var searching = $('.name').children('.address:contains('+comment+')');  
searching.filter(':contains('+comment+')').closest('.name').show();
});

https://jsfiddle.net/4xe28x5z/1/

答案 2 :(得分:1)

您可以使用jQuery :has过滤

&#13;
&#13;
$(document).on('click', '#run', function () {
  var comment = "Street Place";
  var searching = $('.name').has('.address:contains(' + comment + ')');
  searching.show();
});
&#13;
 .name {
   display: none;
 }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="name">
  <p>Mr Sample</p>
  <div class="address">Street Place</div>
</div>
<div class="name">
  <p>Mr Sample 2</p>
  <div class="address">Street Place 2</div>
</div>
<div class="name">
  <p>Mr Sample 3</p>
  <div class="address">Street Palace</div>
</div>
<input type="button" value="Run" id="run" />
&#13;
&#13;
&#13;