jQuery选择器忽略指定的元素
jQuery selector to ignore specified element
考虑这个DOM:
<div id="div1">
<div class="no-select-inside">
<p>Don't select me</p>
</div>
<div>
<p>Select me</p>
<div><p>Select me too</p></div>
</div>
<div>
<p>Select me</p>
<div><p>Select me too</p></div>
</div>
<footer class="no-select-inside">
<p>Don't select me</p>
<div><p>Not me</p></div>
</footer>
<section>
<p>Select me</p>
<div><p>Select me too</p></div>
</section>
</div>
我想要一个快速和可靠jquery(或裸DOM)选择器来选择那些不在类中的p
标记
一切都是动态的,但我可以为不可选的DOM元素分配一个属性。
编辑
真正的测试用例不是一个类选择器(可能是一个复杂的属性选择器,…)
所有东西都是动态的,可能嵌套得太深(在no-select-inside
下的DOM树下有100个p
,它有一个no-select-inside
父级,有了所有答案,即使选择了这些元素)
我缓存了所有的no-select-inside
(Backbone的$el,为了提高性能,可以缓存在数组中),但真正的问题是快速选择这些元素(chrome中20ms太慢了!)。
通用解决方案是从具有.no-select-inside
祖先的解决方案中选择filter
,例如:
$("p")
.filter(function() { return !$(this).closest(".no-select-inside").length;})
// and now do what needs to be done
这应该是相当有效的,因为它只对整个文档进行一次检查。
尝试使用.not()或:not()过滤掉no-select-inside
中的p
元素
$('#div1 p').not('.no-select-inside p')
$('#div1 p:not(.no-select-inside p)')
试试这个:
$('div:not(.no-select-inside) p')
You can use .not as the working example is here
$(document).ready(function(){
var abc = $('#div1').find("p").not('.no-select-inside p');
$(abc).each(function(e){
alert($(this).html());
});
});
http://jsfiddle.net/8F7Kc/14/
相关文章:
- Webdriver.io pageObject模式-通过传递参数来定义元素选择器
- 使用元素选择器向Object添加函数
- j查询未来元素选择器
- Jquery动态元素选择器
- 如何通过元素选择器获取tinyMCE编辑器实例
- 如何在 Edge Animate 中使用类元素选择器绑定 ElementAction
- jQuery在以下选择器中引用初始元素选择器
- 编写一个类似jQuery的JS元素选择器
- 并行元素选择器
- 带有元素选择器的jQuery get()函数
- Jquery Html元素选择器
- 在IE上组合了活动状态伪类和相邻元素选择器
- 仅为通过元素选择器选择的元素获取指定的属性列表
- 为什么jQuery元素[]选择器在这种情况下不起作用?
- jQuery元素选择器
- Javascript元素选择器
- 用于多个元素/选择器的字符语法
- 如何使用Jquery中的元素选择器和类选择器
- 多元素选择器上的jQuery方法链接
- 如何使用jQuery元素选择器从表单中获取输入字段的ID