代码之家  ›  专栏  ›  技术社区  ›  rxmnnxfpvg

YUI 3以编程方式触发更改事件

  •  16
  • rxmnnxfpvg  · 技术社区  · 15 年前

    我想知道如何用yui3以编程方式触发一个变更事件——我向一个选择框节点添加了一个变更监听器:

    Y.get('#mynode').on('change', function(e) {
     Alert(“changed me”);
    });
    

    脚本中的其他地方想触发该事件。当然,当用户更改浏览器中的“选择框”值时,它会起作用。但我已经尝试了很多方法以编程的方式来启动它,但都没有奏效。包括:

    // All below give this error: T[X] is not a function (referring to what's called in .invoke(), // in the minified javascript
    Y.get('#mynode').invoke('onchange');
    Y.get('#mynode').invoke('change');
    Y.get('#mynode').invoke('on','change');
    Y.get('#mynode').invoke("on('change')");
    
    
    /* Tried using .fire() which I found here: 
    * http://developer.yahoo.com/yui/3/api/EventTarget.html#method_fire
    * Nothing happens
    */
    
    Y.get('#mynode').fire('change'); 
    
    /* Looking around the APIs some more, I found node-event-simulate.js: 
     * http://developer.yahoo.com/yui/3/api/node-event-simulate.js.html, 
     * which by its name would seem to have what I want. I tried:
     * Error: simulate(): Event 'change' can't be simulated. 
     * ( (function(){var I={},B=new Date().getTim...if(B.isObject(G)){if(B.isArray(G)){E=1;\n)
     */
    
    Y.get('#mynode').simulate('change');
    

    任何帮助都将不胜感激!

    3 回复  |  直到 15 年前
        1
  •  12
  •   Gabe Moothart    15 年前

    YUI 3.0不支持模拟 change 事件,正如你所发现的。但是,它将在YUI 3.1中得到支持。它是 in the trunk 现在。

    第三次尝试:

    Y.get('#mynode').simulate('change');
    

    应该在3.1中工作。

    编辑

    看起来您可以替换yui 3.0版本的 event-simulate.js 对于Trunk版本,它将在另外一个3.0应用程序中工作。到目前为止我还没有看到任何问题。

        2
  •  6
  •   Matthew Vines    15 年前

    通常的解决方案不是以编程方式触发事件,而是将所有事件逻辑移动到一个函数,并在适当的情况下从代码中调用该函数。

    Y.get('#mynode').on('change', function(e) {
        AlertUserOfChange();
    });
    
    function AlertUserOfChange()
    {
        Alert(“changed me”);
    }
    
        3
  •  -1
  •   SolutionYogi Eric Lippert    15 年前

    这个怎么样?

    Y.Event.simulate('#mynode', 'change');