1
如何獲取Vue.js中被單擊元素的父項? 在jQuery中看起來像這樣:如何獲取Vue.js中單擊元素的父項
$(this).parents(':eq(2)').next('.press__news__nav--view').show();
謝謝!
如何獲取Vue.js中被單擊元素的父項? 在jQuery中看起來像這樣:如何獲取Vue.js中單擊元素的父項
$(this).parents(':eq(2)').next('.press__news__nav--view').show();
謝謝!
當您試圖直接操縱DOM時,在Vue
中這不是一個好習慣。如果你想顯示/隱藏一個元素,你應該使用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
}
}
});
<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>
感謝wostex,它的工作現在。這個項目是特定的,它已經用jQuery製作了。所以,我的工作是重寫Vue。非常感謝! – mmiloshev
@Glavar如果這個答案適合你,你應該接受它。 – Bert