简体   繁体   English

如何从选择器中排除一个 div 元素和他的所有孩子?

[英]How to exclude from the selector * one div element with all his children?

I wrote a script in jquery that adds class to all the elements on the page with the help of the selector * (all) .我在 jquery 中编写了一个脚本,借助选择器 * (all)将 class 添加到页面上的所有元素中。 I would like to rule out one element of the div with all his children.我想与他的所有孩子一起排除 div 的一个元素。 I'm wondering how to do that.我想知道该怎么做。 I tried to use a pseudo-class:not , including the mentioned div , but I don't know how to exclude his children as well.我尝试使用伪类:not包括提到的 div ,但我也不知道如何排除他的孩子。 Can any of you help me solve this?你们中的任何人都可以帮我解决这个问题吗?

Code with pseudo-class:not带有伪类的代码:不是

$(document).ready(function(){
   $('#yellow-background').click(function(){
      $(':not(div[aria-label="Slajder"])').addClass("yellow-background");
});

Normal code:正常代码:

$(document).ready(function(){
   $('#yellow-background').click(function(){
      $('*').addClass("yellow-background");
});

ALL elements?所有元素? Why not make a yellow overlay and show the div in the middle of that instead ?为什么不做一个黄色叠加层并在中间显示 div呢?

Anyway反正

$("*:not('div[aria-label=Slajder] *')").addClass("yellow-background");

is harder to write than比写更难

$('*').not('div[aria-label="Slajder"] *').addClass("yellow-background");

due to quotes but both work由于报价,但两者都有效

 $(function() { $('#yellow-background').click(function() { $('*').not('div[aria-label="Slajder"] *').addClass("yellow-background"); }); });
 .yellow-background { background-color: yellow } [aria-label=Slajder] { background-color: red }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="yellow-background">Yellow</button> <div>Content <div>content <div aria-label="Slajder"><h1>Slajder </h1> <div> exluded content <div>more exluded content</div> </div> </div> more content </div> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM