【问题标题】:How to limit jQuery searches to a certain area如何将 jQuery 搜索限制在某个区域
【发布时间】:2012-03-17 23:33:05
【问题描述】:

假设我有以下内容。

<div class="foo">
<div>
some text
<div class="bar">
</div>
</div>
</div>

<div class="foo">
<div>
some text
<div class="bar">
some text
</div>
</div>
</div>

我想返回在 div 类“bar”中有“一些文本”的类“foo”的所有 div。所以第二个会被返回,但不是第二个。我该怎么做?

【问题讨论】:

    标签: javascript jquery html parsing tags


    【解决方案1】:

    试试这个

    $("div.bar:contains('some text')").parents(".foo")
    

    【讨论】:

      【解决方案2】:

      这样就可以了

      $('.foo:has(.bar:not(:empty))')
      

      确保 .bar 内没有字符,甚至是空格或换行符。

      http://jsfiddle.net/Nk4FB/

      【讨论】:

      • some text 部分是什么?
      • 这将返回所有 .foo 的后代 .bar 不为空。因此,如果“某些文本”在 bar 中,它将返回其父 foo。
      猜你喜欢
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2016-10-26
      • 2018-08-01
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多