使用vue.js获取调用元素 [英] Get the calling element with vue.js

查看:83
本文介绍了使用vue.js获取调用元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在vue.js中调用html元素来通过jQuery修改它。现在我给每个元素提供类名+索引,然后通过jQuery调用它,但这看起来像是一个疯狂的黑客。

I want to get the calling html element in vue.js to modify it via jQuery. For now I give every element the class name + the index and call it via jQuery afterwards, but this looks like a crazy hack.

我想做什么:

new Vue({
    el: "#app",
    data: {  
        testFunction : function(element) {
            $(element).doSomethingWithIt(); //do something with the calling element
        }
    }
});

这是调用元素:

<div v-on:click="testFunction(???)">Test</div> 

我可以将什么传递给函数来获取div元素,还是有另一种方法来实现这一点?

What can I pass into the function to get the div-element or is there another way to achieve this?

推荐答案

您可以从这样的事件中获取元素:

You could get the element from the event like this:

new Vue({
    el: "#app",
    methods: {  
        testFunction : function(event) {
            $(event.target).doSomethingWithIt();
        }
    }
});

然后:

<div v-on:click="testFunction">Test</div>

或(如果你想传递另一个参数):

Or (if you want to pass another parameter):

<div v-on:click="testFunction($event)">Test</div>

[demo]

这篇关于使用vue.js获取调用元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆