Javascript:模拟链接点击

发布于 2024-07-14 21:55:06 字数 286 浏览 4 评论 0原文

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

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

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

$('Element').click()

谢谢。

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()

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

誰認得朕 2024-07-21 21:55:06

MDC 有一个很好的使用dispatchEvent 的示例来模拟点击事件。

以下是一些模拟单击元素的代码,该代码还检查是否有某些内容取消了事件:

function simulateClick(elm) {
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
    0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var canceled = !elm.dispatchEvent(evt);
  if(canceled) {
    // A handler called preventDefault
    // uh-oh, did some XSS hack your site?
  } else {
    // None of the handlers called preventDefault
    // do stuff
  }
}

MDC has a good example of using dispatchEvent to simulate click events.

Here is some code to simulate a click on an element that also checks if something canceled the event:

function simulateClick(elm) {
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
    0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var canceled = !elm.dispatchEvent(evt);
  if(canceled) {
    // A handler called preventDefault
    // uh-oh, did some XSS hack your site?
  } else {
    // None of the handlers called preventDefault
    // do stuff
  }
}
手心的海 2024-07-21 21:55:06

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

对于 YUI 3,这一切都很好地包装在 Y.Event.simulate() 中

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

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

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 })
})
○愚か者の日 2024-07-21 21:55:06

您可以单独声明您的函数。

function DoThisOnClick () {
}

然后像现在一样将其分配给 onclick 事件,例如:

YAHOO.util.Event.on(Element, 'click', DoThisOnClick)

您可以随时调用它:-)

DoThisOnClick ()

You can declare your function separately.

function DoThisOnClick () {
}

Then assign it to onclick event as you do right now, e.g.:

YAHOO.util.Event.on(Element, 'click', DoThisOnClick)

And you can call it whenever you want :-)

DoThisOnClick ()
忘你却要生生世世 2024-07-21 21:55:06

如果有人遇到这个问题,正在寻找一种与框架无关的方式来触发任何 HTML 和鼠标事件,请查看此处:如何使用 JavaScript 模拟鼠标点击?

In case anyone bumps into this looking for a framework agnostic way to fire any HTML and Mouse event, have a look here: How to simulate a mouse click using JavaScript?

无语# 2024-07-21 21:55:06

1)第一个解决方案

文章 http://mattsnider.com/simulated-events-using-yui/ 描述了如何使用 YAHOO 模拟点击:

var simulateClickEvent = function(elem) {
    var node = YAHOO.util.Dom.get(elem);

    while (node && window !== node) {
        var listeners = YAHOO.util.Event.getListeners(node, 'click');

        if (listeners && listeners.length) {
            listeners.batch(function(o) {
                o.fn.call(o.adjust ? o.scope : this, {target: node}, o.obj);
            });
        }

        node = node.parentNode;
    }
};

如您所见,该函数在节点及其父节点上循环,并为每个节点获取侦听器列表并调用它们。

2)第二种解决方案

还有另一种方法可以做到这一点。
例如:

var elem = YAHOO.util.Dom.get("id-of-the-element");
elem.fireEvent("click", {});

其中函数用作

3) 第三种解决方案

YUI2 的 2.9 版本有更好的解决方案: http://yui.github.io/yui2/docs/yui_2.9.0_full/docs/YAHOO.util.UserAction.html

4) 第四种解决方案

YUI3 还提供了更好、更干净的解决方案: http://yuilibrary.com/yui/docs/event/simulate .html

1) FIRST SOLUTION

The article http://mattsnider.com/simulating-events-using-yui/ describes how to simulate a click using YAHOO:

var simulateClickEvent = function(elem) {
    var node = YAHOO.util.Dom.get(elem);

    while (node && window !== node) {
        var listeners = YAHOO.util.Event.getListeners(node, 'click');

        if (listeners && listeners.length) {
            listeners.batch(function(o) {
                o.fn.call(o.adjust ? o.scope : this, {target: node}, o.obj);
            });
        }

        node = node.parentNode;
    }
};

As you can see, the function loops over the node and its parents and for each of them gets the list of listeners and calls them.

2) SECOND SOLUTION

There is also another way to do it.
For example:

var elem = YAHOO.util.Dom.get("id-of-the-element");
elem.fireEvent("click", {});

where function is used as

3) THIRD SOLUTION

Version 2.9 of YUI2 has a better solution: http://yui.github.io/yui2/docs/yui_2.9.0_full/docs/YAHOO.util.UserAction.html

4) FORTH SOLUTION

YUI3 has also a better and clean solution: http://yuilibrary.com/yui/docs/event/simulate.html

不羁少年 2024-07-21 21:55:06

当然 $('Element').click() 不起作用,但如果你包含 jquery,它就可以,它与 yui 一起工作得很好。

当我进行测试时,您需要执行以下操作:

function myfunc(){
//functionality
}

YAHOO.util.Event.on(Element, 'click', myfunc);

然后在需要发生其他事情时调用 myfunc。

Of course $('Element').click() won't work, but it can if you include jquery, it works well alongside yui.

As I untestand you need to do the following:

function myfunc(){
//functionality
}

YAHOO.util.Event.on(Element, 'click', myfunc);

Then call myfunc when something else needs to happen.

夜声 2024-07-21 21:55:06

无法模拟用户点击任意元素是故意的,原因很明显。

The inability to simulate a user-click on an arbitrary element is intentional, and for obvious reasons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文