我想知道如何使用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');
任何帮助都将不胜感激!
发布于 2009-07-31 21:44:16
通常的解决方案不是以编程方式触发事件,而是将所有事件逻辑移动到一个函数,然后在适当的地方从代码中调用该函数。
Y.get('#mynode').on('change', function(e) {
AlertUserOfChange();
});
function AlertUserOfChange()
{
Alert(“changed me”);
}
发布于 2009-07-31 21:51:38
这个怎么样?
Y.Event.simulate('#mynode', 'change');
https://stackoverflow.com/questions/1215012
复制相似问题