1、原生写法
// html
<div id="box" style="width:110px;height:110px;background-color:red"></div>
//js
document.addEventListener('click',(e)=>{
alert('zhixing')
var box = document.getElementById('box');
if(box.contains(e.target)){
alert('在内');
}else{
alert('在外');
}
})
2、 vue 写法
//html
<div id="box" ref="box" style="width:110px;height:110px;background-color:red"></div>
//js
created(){
document.addEventListener('click',(e)=>{
console.log(this.$refs.box.contains(e.target));
if(!this.$refs.box.contains(e.target)){
this.isShowDialog = false;
}
})
}