我如何选择里面的所有元素.content而不是.bts呢?这是我的代码。我正在使用not()要排除的功能,.bts但无法正常工作。应该做什么?
$('.content').not('.bts').click(function() {
alert("hit");
});Run Code Online (Sandbox Code Playgroud)
.content {
height: 100%;
width: 100%;
}
p {}
.bts {
background: blue;
height: 100px;
width: 100px;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
您可以使用 $('.content *:not(.bts)')
.content *表示将使用上的所有元素.content,添加not(.bts)会bts class从规则中删除所有带有的元素。
演示版
$('.content *:not(.bts)').click(function() {
console.log("hit");
});Run Code Online (Sandbox Code Playgroud)
.content {
height: 100%;
width: 100%;
}
p {}
.bts {
background: blue;
height: 100px;
width: 100px;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)