[removed] simulate a click on a link

前端 未结 7 2038
耶瑟儿~
耶瑟儿~ 2020-12-20 21:39

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

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

7条回答
  •  醉酒成梦
    2020-12-20 22:28

    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

提交回复
热议问题