Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

vue.js如何如何实现点击document 让隐藏的下拉框功能

1。自己写一个下拉框的组件,但是没有实现点击document让下拉框消失的功能,

<template>
<div class="choose-slidedown" @click.stop="showItem">
<p :class="{choosed: bChoosed}" :style="pStyle">
<span>{{choosedLabel}}</span>
</p>
<ul class="list" v-if="bVisible">

<li v-for="item in useroptions" :class="{active: item.bActive}" @click="chooseItem(item)" >{{item.label}}</li>

</ul>
</div>
</template>

export default {
name: 'slidedown',
props:
{

pStyle: {
  type: Object
},
arrowStyle: {
  type: Object
},
useroptions: {
  type: Array,
  default () {
    return [{
      value: '100',
      label: '100万',
      bActive: false
    }, {
      value: '200',
      label: '200万',
      bActive: false
    }, {
      value: '300',
      label: '300万',
      bActive: false
    }, {
      value: '500',
      label: '500万',
      bActive: false
    }, {
      value: '1000',
      label: '1000万',
      bActive: false
    }]
  }
}

},
data () {

return {
  value: '',
  label: '请选择',
  bVisible: false,
  bChoosed: false
}

},
computed: {

choosedLabel () {
  var temp = this.useroptions.filter(item => { return item.bActive === true })
  if (temp[0]) {
    return temp[0]['label']
  } else {
    return '请选择'
  }
}

},
methods: {

chooseItem: function (item) {
  this.useroptions.map(item => { item.bActive = false })
  item.bActive = true
  this.value = item.value
  this.label = item.label
  this.bChoosed = true
  this.$emit('receiveData', this.label, this.value)
},
showItem () {
  this.bVisible = !this.bVisible
}

},
created () {

// 现在还在测试中
document.onclick = () => {
  this.$set(this.$data, 'bVisible', false)
  console.log(this.$data.bVisible)
}

}
}
</script

但是现在底部的document.onclick = () => {
this.bVisible = false
}
生效了,但是下拉框就是不隐藏,该怎么解决?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

试试用@blur 这个失去焦点的事件是不是符合你的要求.对了如果要在div上用给div加tabindex="1"

测试的例子


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...