如果我单击.m1wrap
子项,则会出现此警报,但如果在除.m1wrap
及其所有子项之外的任何位置都有单击,则我需要显示警报。
<div class="m1wrap">
<div class="m1top"></div>
<div class="m1bottom"></div>
</div>
js
$(document).on("click", function(e) {
if (e.target.className !== "m1wrap") {
alert ("323");
};
})
尝试使用.is()
和选择器".m1wrap, .m1wrap *"
来选择父.m1wrap
和.m1wrap
的子级
$(document).on("click", function(e) {
if (!$(e.target).is(".m1wrap, .m1wrap *")) {
alert ("323");
};
})
.m1wrap, .m1wrap * {
border: 2px solid tomato;
width:100px;
padding:2px;
}
.m1top {
position:absolute;
left:120px;
}
.m1bottom {
position:absolute;
left:250px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="m1wrap">
m1wrap
<div class="m1top">m1top</div>
<div class="m1bottom">m1bottom</div>
</div>