YUI3:我该如何触发事件? [英] YUI3: How would I trigger an event?

查看:77
本文介绍了YUI3:我该如何触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在链接/按钮/等上有一个点击事件.

Suppose I have a click event on a link/button/etc.

var myButton = Y.one('.button');

myButton.on('click', function() {
   // code
});

我想在此按钮上触发点击事件,页面上还有其他事情在发生.我该怎么办?

There is something else happening on the page that I want to trigger a click event on this button. How would I do this?

我看到了YUI3的fire()方法,但它看起来像是为自定义事件设计的.如果我应该使用fire(),那么myButton.fire('click')是否可以工作?

I saw YUI3's fire() method, but it looked like that was designed for custom events. If I am supposed to use fire(), then will myButton.fire('click') work?

(我正在寻找与jQuery的.trigger()方法等效的方法,该方法适用于DOM事件或自定义事件.)

(I'm looking for the equivalent of jQuery's .trigger() method, which works on DOM events or custom events.)

推荐答案

您真的需要触发按钮上的click事件吗?取下面的HTML

Do you really need to trigger the click event on the button? Take the HTML below

    <button id="myButton">Click Me</button>
<br>
    <a href="#">Click Me</a>

您可以利用自定义事件将真实的逻辑放在中心位置.

You can make use of the custom events to put the real logic somewhere central.

YUI().use("node","event",function(Y){
    Y.one("#myButton").on("click",function(){Y.fire("custom:doThing")});
    Y.all("a").on("click",function(){Y.fire("custom:doThing")});
    Y.on("custom:doThing",function(){console.log("Do my thing, regardless of event source")})
});

提琴: http://jsfiddle.net/WZZmR/

这篇关于YUI3:我该如何触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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