javascript - 排除 DOM 子树的 jQuery 选择器

标签 javascript jquery jquery-selectors

是否有一个 jQuery 选择器可以获取 A 类中不是 B 类后代的所有元素?

示例:

<body>
   <div class=report-value id=overview></div>
   <div class=panels>
      <div class=report-value id=sales></div>
      <div class=report-value id=training></div>
      <div class=report-value id=hr></div>
   </div>
   <div class=report-value id=summary></div>
</body>

对于上面的示例,需要选择不是 .panels 元素后代的所有 .report-value 元素。报告值的计算量很大,只有在实际显示时才需要计算。

类似于:

var elems = $('.report-value:excludeTree(.panels)');

它将返回一个仅包含#overview#summary的jQuery对象。

性能对于此 Web 应用程序非常重要。

最佳答案

您可以使用.not()过滤掉这些元素

$('.report-value').not('.panels .report-value')

演示:Fiddle

关于javascript - 排除 DOM 子树的 jQuery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21770531/

相关文章:

javascript - Math.min.apply(Math,Array) 当 Array.length = 100 时返回 NaN

javascript - 如何确保在 HREF 触发之前下载并显示图像?

jQuery 查询选择器 : is there benefit to selecting by class AND id?

javascript - 突出显示子节点的 TreeMap D3

javascript - 是否可以显示所有事件的 jQuery bind()?

javascript - 用于否定父元素中特定 HTML 元素的 JQuery 选择器

jquery - 查找和隐藏元素 (jQuery)

javascript - attributeContains 选择器中的表达式无法识别

javascript - 如何将 HandsOnTable 注释设置为只读

javascript - 在 asp.net mvc 中对 Controller 进行简单的 Ajax 调用