我有一个下拉按钮
组件,它有一个点击监听器。该按钮包含文本和图标。点击事件仅在我们小心地在轮廓上而不是在文本或图标上单击按钮时触发。这是我的组件:
<template lang="html">
<div>
<button class="button dropbtn" @click="toggleDropdown">
<span>{{ name }}</span>
<span class="icon"><i class="fa fa-caret-down"></i></span>
</button>
<div v-show="visible" class="dropdown-content is-primary-important">
<slot>
</slot>
</div>
</div>
</template>
<script>
export default {
props: ['name'],
data: function() {
return {
visible: false
}
},
mounted: function() {
this.hideDropdownOnClick();
},
methods: {
toggleDropdown: function() {
// trigged on click of the button
// make the dropdown visible
console.log("toggling dropdown");
this.visible = !this.visible;
},
hideDropdownOnClick: function() {
window.onclick = (event) => {
console.log(event.target);
if (!event.target.matches('.dropbtn')) {
console.log("here", this.visible);
this.visible = false;
}
}
}
}
}
</script>
<style lang="css">
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
position: absolute;
background-color: #fff;
min-width: 140px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
padding: 12px 16px;
text-decoration: none;
display: block;
}
</style>
我感觉我在这里缺少一些非常基础的东西,有人能帮帮我吗?谢谢。
编辑
似乎是浏览器的一个错误,这个问题的答案如下: 点击按钮文本并释放仍然在按钮内时,按钮元素不会触发点击事件?
chrome
中它无法工作! - aks