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

atp*_*atp 16 events onchange yui3

我想知道如何以编程方式使用YUI3触发更改事件 - 我向一个选择框节点添加了一个更改侦听器:

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

脚本中的其他地方想要触发该事件.当然,当用户更改浏览器中的选择框值时,它可以工作.但我已经尝试了很多方法来以编程方式启动它,但没有一种方法可行.包含:

// 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');
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激!

Gab*_*art 12

change正如您所发现的,YUI 3.0不支持模拟事件.但是它将在YUI 3.1中得到支持.它现在在行李箱里.

你的第三次尝试:

Y.get('#mynode').simulate('change');
Run Code Online (Sandbox Code Playgroud)

应该在3.1中工作.

编辑

看起来你可以event-simulate.js用主干版本替换YUI 3.0版本,它将在另一个3.0应用程序中工作.到目前为止我还没有看到任何问题.


Mat*_*nes 6

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

Y.get('#mynode').on('change', function(e) {
    AlertUserOfChange();
});

function AlertUserOfChange()
{
    Alert(“changed me”);
}
Run Code Online (Sandbox Code Playgroud)