【问题标题】:JQuery Selector for not "wrapped" elements用于未“包装”元素的 JQuery 选择器
【发布时间】:2012-08-09 02:43:55
【问题描述】:

我正在寻找一个 JQuery 选择器,它选择所有没有被特定 div 包装的 img 元素(使用 .special 或 .dontSelect 类)

例如我的html代码:

<div class="special">
    <ul>
        <li>
             <img id="1"></img>
        </li>
    </ul>
</div>
<div>
    ....
          <img id="2"></img>
    ....
          <div class="dontSelect">
              <img id="3"></img>
          </div>
</div>

在这个例子中,只有 id 为 2 的 img 应该被选中,因为 1 和 3 被“包装”了。

感谢您的帮助

【问题讨论】:

    标签: jquery jquery-selectors


    【解决方案1】:

    试试:

     $('*:not(.dontSelect), *:not(.special)').find('img')
    

    甚至

     $('*:not(.dontSelect, .special) img')
    

    【讨论】:

      【解决方案2】:

      没有选择器,但您可以选择所有图像并过滤它们:

      $("img").filter( function(){
          return !$(this).closest(".special,.dontSelect").length
      }).remove();
      
      //Will remove the image with id="2"
      

      【讨论】:

        【解决方案3】:

        您可以使用filter 方法:

        var $imgs = $('img').filter(function(){
                       return $(this).closest('.dontSelect, .special').length === 0
                   })
        

        【讨论】:

          猜你喜欢
          • 2016-02-28
          • 1970-01-01
          • 2023-04-02
          • 2011-01-05
          • 2015-01-03
          • 2013-02-03
          • 2019-05-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多