html - 如何从选择器中排除一个 div 元素和他的所有孩子?
问题描述
我在 jquery 中编写了一个脚本,在选择器 * (all)的帮助下将类添加到页面上的所有元素。我想与他的所有孩子一起排除 div 的一个元素。我想知道该怎么做。我尝试使用伪类 :not,包括提到的 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");
由于报价,但两者都有效
$(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>
推荐阅读
- android - 单击时更改列表视图项目的背景颜色 - 并记住它
- android - 如何在 android 上解码 m4a 音频
- google-sheets - 谷歌表格双数组查找
- arrays - Leetcode 867. 转置矩阵;调试;生成列表
- python - 如何使用散点图大小将 x,y 值的频率表示为每个 x 值总数的百分比?
- c# - IdentityServer4 AddSigningCredentials 和证书
- html - 从css将div定位到botton
- php - 使用 imagejpeg() 保存裁剪后的图像第二次同名
- c# - 在 compareTo 中检查参数的有效性?
- java - 优化事件处理