vue中如何实现点击除了自己元素之外都将自身元素隐藏的功能

本文介绍了在Vue中实现点击除自身元素外其他地方隐藏元素的多种方法,包括使用`$refs`判断点击目标、事件修饰符控制事件传播、原生JavaScript的`contains`方法检查点击位置以及jQuery的事件监听和阻止传播。详细展示了不同写法的代码示例,涵盖了从基础到进阶的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、vue的写法1

//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;
        }
    })
}

2、vue的写法2

 // 检查一个鼠标事件是否发生在元素内部(含border,不含box-shadow等)

HTML:

<li v-for="(twonav,indextwo) in nav.child.contentItems" :key="indextwo">

<button :style='{background:twonav.color}' @click.stop="showThree(twonav,twonav.contentItemId)" @mouseover.stop="showTwodDescription(twonav,twonav.contentItemId)" @mouseout.stop ="hideOneDescription()">{{twonav.displayText}}</button>

<div class="describe" :class="returnTwoNum == twonav.contentItemId? 'twoclassdescrption' : ''">

<span v-if="twonav.description"> {{twonav.description}}</span><span v-else>暂无描述信息</span>

</div>

 

<div class="class-three" :class="returnThreeNum == twonav.contentItemId? 'threeclassdescrption' : ''" >

<div class="threebg">

<p v-for="(threenav,indexthree) in twonav.child.contentItems" :key="indexthree">

<button :style='{background:threenav.color}' @mouseover.stop="showThreeDescription(threenav.contentItemId)" @mouseout.stop ="hideOneDescription()">{{threenav.displayText}}</button>

<span class="describe" :class="returnThreeNums == threenav.contentItemId? 'twoclassdescrption' : ''"><em v-if="threenav.description"> {{threenav.description}}</em><em v-else>暂无描述信息</em></span>

</p>

</div>

</div>

</li>

CSS:

.twoclassdescrption{

   display: block !important;

}

JS:

data () {

returnThreeId:"",

},

computed:{

      return this.returnThreeId

},

methods:{

//点击二级导航是否显示三级导航信息

showThree(nav,id){

    this.returnThreeId = id;

},

isClickOutsideEl(evt, domEl) {
      let rect = domEl.getBoundingClientRect();
      let clientX = evt.clientX;
      let clientY = evt.clientY;

      return (
        clientX >= rect.left &&
        clientX <= rect.left + rect.width &&
        clientY >= rect.top &&
        clientY <= rect.top + rect.height
      );
    },

      // 是否点击目标区域外 -> 关闭下拉
      docClickDebouncer(evt) {
        if (!this.isClickOutsideEl(
          evt,
          this.$el.querySelector(".class-three")
        )) {
          this.returnThreeId = null;
        }
      },
},

watch: {
    returnThreeNum(val,old){
      if(val !== old){
        $(document).on("click", this.docClickDebouncer);
      } else {
        $(document).off("click", this.docClickDebouncer);
      }
    }
  }


3、原生js的写法

// 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('在外');        
        }
    })

4、jq最简单的写法

$(document).click(function(){

   $("#element").hide();

});

$("#element").on("click",function(event){

    event.stopPropagation();

});

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值