Javascript:模拟链接上的点击 [英] Javascript: simulate a click on a link

查看:104
本文介绍了Javascript:模拟链接上的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个附加了侦听器的链接(我正在使用YUI):

I have a link that has a listener attached to it (I'm using YUI):

YAHOO.util.Event.on(Element, 'click', function(){ /* some functionality */});

我希望在不涉及用户点击的另一个场景中发生相同的功能。理想情况下,我可以模拟元素上的点击并自动触发功能。我怎么能这样做?
太糟糕了,这不起作用:

I would like to the same functionality to happen in another scenario that doesn't involve a user-click. Ideally I could just simulate "clicking" on the Element and have the functionality automatically fire. How could I do this? Too bad this doesn't work:

$('Element').click()

谢谢。

推荐答案

您正在寻找 fireEvent (IE )和 dispatchEvent (其他)。

You're looking for fireEvent (IE) and dispatchEvent (others).

对于YUI 3,这完全包含在 Y.Event.simulate()

For YUI 3 this is all wrapped up nicely in Y.Event.simulate():

YUI().use("node", function(Y) {
    Y.Event.simulate(document.body, "click", { shiftKey: true })
})

这篇关于Javascript:模拟链接上的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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