题
我想知道如何以编程方式使用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');
任何帮助将不胜感激!
解决方案
YUI 3.0不支持模拟 change
事件,正如您所发现的那样。但是它将在YUI 3.1中得到支持。现在在行李箱中。
你的第三次尝试:
Y.get('#mynode').simulate('change');
应该在3.1中工作。
修改强>
看起来你可以用trunk版本替换YUI 3.0版本的 event-simulate.js
,它将在3.0应用程序中运行。到目前为止我还没有看到任何问题。
其他提示
通常的解决方案不是以编程方式触发事件,而是将所有事件逻辑移动到一个函数,而是在适当的时候从代码中调用该函数。
Y.get('#mynode').on('change', function(e) {
AlertUserOfChange();
});
function AlertUserOfChange()
{
Alert(“changed me”);
}
这个怎么样
Y.Event.simulate('#mynode', 'change');
不隶属于 StackOverflow