如何在Vue.js中获取clicked(this)元素的父元素?在jQuery中,这看起来像:
$(this).parents(':eq(2)').next('.press__news__nav--view').show();
Run Code Online (Sandbox Code Playgroud)
谢谢!
Ego*_*kio 29
Vue当你试图直接操作DOM时,这不是一个好习惯.如果要显示/隐藏元素,则应使用v-if或v-show指令.
无论如何,你可以event像这样访问对象:
new Vue({
el: '#app',
methods: {
logme: function(event) { // a regular event object is passed by $event in template
console.log(event.target.parentElement) // parent element
}
}
});Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="app">
<div class="hello">
<button @click="logme($event)">Click me</button>
</div>
</div>Run Code Online (Sandbox Code Playgroud)