javascript - 如何将 vue.js 事件附加到现有 html 元素

标签 javascript vue.js

有没有办法将 vue.js 事件附加到现有的 html 元素?

HTML 元素:

<div class="borrow-button">Borrow</div>
<div class="borrow-button">Borrow</div>
<div class="borrow-button">Borrow</div>

我可以以某种方式向此 div 添加 vue.js 事件吗?

$('.borrow-button').each(function(i, obj) {
    //how to append for example onclick event to this divs? (onclick="borrow")
});

最佳答案

我目前不知道有任何 vue 方法可以做到这一点。但由于 jsgrid 使用 jquery,因此您可以使用 event handler attachment它来绑定(bind)vue方法。

这是一个例子:

var demo = new Vue({
    el: '#demo',
    data: function(){
        return {
      };
    },
    methods:{
      myMethod () {
        alert('Do whatever you wanns do')
      }
    },
    mounted () {
        $("#myBtn").on('click', this.myMethod);
    }
})

您可以检查此代码是否正常工作 here .

关于javascript - 如何将 vue.js 事件附加到现有 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41183388/

相关文章:

javascript - 使用 window.location.hostname 时生成意外的 URL

vue.js - 结果、Vue、Apollo 和 GraphQL 缺少属性

javascript - 如何在滚动条上改变div宽度

javascript - jQuery或Java声音通知/警报

javascript - AngularJS 的 .config()

javascript - 无法让 $emit 将 prop 传递给 vuejs 中的父级

javascript - 在 AngularJS 中的 Controller 之间共享来自 API 的数据

javascript - 下拉选择后,存储选择并使用 mysql 数据库的结果刷新同一页面

javascript - 等待带有 v-card 的 v-dialog 需要 Vue.js 的透明背景

express - Vue + Express(NestJS) 不可能完成的任务,您正在使用 Vue 的仅运行时版本,其中