简体   繁体   English

选择除一个及其子/孙之外的所有元素

[英]Selecting all elements except one and its children/grandchildren

I am looking for a way to select all elements except one element and its descendant which might have children/grandchildren or even more. 我正在寻找一种方法来选择除一个元素及其后代之外的所有元素,这些元素可能有子/孙子甚至更多。 What I exactly wanna do is something like... 我真正想做的是......

$("*").not(".foo, .foo *").bind("touchmove",function(e){
    e.preventDefault();
});

this would disable all touchmove event except foo class and its children. 除了foo类及其子类之外,这将禁用所有touchmove事件。 But unfortunately I don't know how many generations it would have because the code I'm about to write would be used in alot of templates file so they might have no children at all or maybe they have 10 generations family. 但不幸的是,我不知道它会有多少代,因为我要编写的代码将被用于很多模板文件中,因此它们可能根本就没有孩子,或者他们可能拥有10代家庭。

is there any way to achive this? 有没有办法实现这个? other than making new dev/span to wrap everyone else but the one I don't wanna select.(this solution would take really long time for some reason.) 除了制作新的dev / span以包装其他所有人,而不是我不想选择的那个。(这个解决方案需要很长时间才能出于某种原因。)

any suggestion or recommendation would be appreciated. 任何建议或建议将不胜感激。

Your code does exactly what you want: 您的代码完全符合您的要求:

JSFiddle example JSFiddle示例

Have you tried it? 你试过吗? I made a small example with hover to make it visible + I selected only div's and p's to make it easier, but it will work with all the elements as well. 我用悬停来做一个小例子让它可见+我只选择了div和p来使它更容易,但它也适用于所有元素。

$("div,p").not(".foo, .foo *").hover(function(){
    $(this).css("background", "#70A5C9");
},function(){
    $(this).css("background", "#ffffff");
});

It works because every descendant inherits the classes of his ancestors. 它的工作原理是因为每个后代都继承了他祖先的类。 In my example you can see that the deepest div will have all the fooX class ( 1 < X < 6). 在我的例子中,你可以看到最深的div将拥有所有的fooX类(1 <X <6)。

See another visible example , once I give foo3 a new background color, all the inner foos will have the same background color. 看另一个可见的例子 ,一旦我给foo3一个新的背景颜色,所有内部foos将具有相同的背景颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript选择除了一个及所有子元素之外的所有元素? - How to select all elements, except one and all its children using JavaScript? 如何解析文档并删除除匹配和ID及其子项之外的所有元素 - How can I parse a document and remove all elements except for one that matches and ID and its children JS删除除特定ID及其子元素之外的所有元素 - JS Remove all elements except a specific ID and its children 选择除一个之外的所有div - Selecting all divs except one JS:隐藏除了一个(由id)以及它的所有降序子元素及其父元素之外的所有元素 - JS: Hide all elements except of one (by id) (and all of its descending child elements and its parents) 选择除兄弟姐妹和自我之外的所有元素 - Selecting all elements EXCEPT siblings and self 通过jquery选择元素曾孙并改变其宽度 - Selecting elements great grand children by jquery and changing its width 注册单击除div及其所有子项以外的所有内容 - register click on everything on body except div and all its children jQuery / javascript隐藏除单击项外的所有子窗体元素 - jquery/javascript hide all children form elements except the clicked item jQuery将类设置为所有元素,但具有特定类的元素+ children / children的孩子 - Jquery set class to all elements except element with specific class + children/children of children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM