【问题标题】:How can I trigger Vue event from jquery?如何从 jquery 触发 Vue 事件?
【发布时间】:2015-11-18 07:51:32
【问题描述】:

如何从 jquery 触发 Vue 事件?我有

<input type='text' v-on='keypress: doSomthing()' id='demo'>

如何在 vue 之外触发此事件 .. 使用 jquery?我以为我能做到

$('#demo').trigger('keypress')` 

...但它不会触发事件。它什么也不做。知道怎么做吗?

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    Vue 正在挂钩原生事件。 jQuery 在原生事件之上的层中生成事件。有关如何使用 document.createEvent 创建和调度原生事件的更多信息,请参阅此处:

    https://learn.jquery.com/events/triggering-event-handlers/

    这里有一个很好的例子:Is it possible to simulate key press events programmatically?

    【讨论】:

    • 非常感谢。 jquery.simulate.js 确实成功了。我可以有 并使用 $('#x').simulate('keypress') 调用它
    猜你喜欢
    • 2022-10-20
    • 2018-02-09
    • 2021-01-20
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2019-02-27
    相关资源
    最近更新 更多