[英]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: 您的代码完全符合您的要求:
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.