我在jquery中编写了一个脚本,它在选择器*(all)的帮助下为页面上的所有元素添加类。 我想排除他所有孩子的div中的一个元素。 我在想怎么做。 我试图使用一个伪类:不,包括提到的div,但我不知道如何排除他的孩子以及。 你们谁能帮我解决这个?
带有伪类的代码:not
$(document).ready(function(){
$('#yellow-background').click(function(){
$(':not(div[aria-label="Slajder"])').addClass("yellow-background");
});
普通代码:
$(document).ready(function(){
$('#yellow-background').click(function(){
$('*').addClass("yellow-background");
});
所有元素? 为什么不制作一个黄色的覆盖图,并在中间显示div呢?
无论如何
$("*:not('div[aria-label=Slajder] *')").addClass("yellow-background");
比写起来更难
$('*').not('div[aria-label="Slajder"] *').addClass("yellow-background");
由于引号,但两者都有效
null
$(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>