YUI 3 以编程方式触发更改事件
我想知道如何使用 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');
任何帮助将不胜感激!
I was wondering how to programmatically fire a change event with YUI3 -- I added a change listener to one select box node:
Y.get('#mynode').on('change', function(e) {
Alert(“changed me”);
});
and somewhere else in the script want to fire that event. It works, of course, when a user changes the select box value in the browser. But I've tried many ways to fire it programmatically, none of which have worked. Including:
// 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');
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常的解决方案不是以编程方式触发事件,而是将所有事件逻辑移动到函数中,并在适当的情况下从代码中调用该函数。
The usual solution is not to programmatically fire the event, but rather move all the event logic to a function, and instead call that function from your code where appropriate.
这个怎么样
How about this
正如您所发现的,YUI 3.0 不支持模拟
change
事件。 不过 YUI 3.1 将支持它。 现在在主干中。您的第三次尝试:
应该在 3.1 中工作。
编辑
看起来您只需将
event-simulate.js
的 YUI 3.0 版本替换为 trunk 版本,它就可以在其他 3.0 应用程序中运行。 到目前为止我还没有看到任何问题。YUI 3.0 does not support simulating the
change
events, as you've discovered. However it will be supported in YUI 3.1. It is in the trunk now.Your third attempt:
should work in 3.1.
edit
It looks like you can just replace the YUI 3.0 version of
event-simulate.js
with the trunk version, and it will work in an otherwise 3.0 app. I haven't seen any issues so far.