如何选择 .content
而不是 .bts
中的所有元素?这是我的代码。我正在使用 not()
函数来排除 .bts
但它不起作用。应该做什么?
$('.content').not('.bts').click(function() {
alert("hit");
});
.content {
height: 100%;
width: 100%;
}
p {}
.bts {
background: blue;
height: 100px;
width: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
<p>test</p>
<div class="bts">bts</div>
</div>
最佳答案
你可以使用$('.content *:not(.bts)')
.content *
表示它将获取 .content
上的所有元素,添加 not(.bts)
会删除带有 bts
class
来自规则。
演示
$('.content *:not(.bts)').click(function() {
console.log("hit");
});
.content {
height: 100%;
width: 100%;
}
p {}
.bts {
background: blue;
height: 100px;
width: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
<p>test</p>
<div class="bts">bts</div>
</div>
关于javascript - 选择 div 内的所有元素,但不选择特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56596218/