首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >YUI 3以编程方式触发更改事件

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

Stack Overflow用户
提问于 2009-07-31 21:35:52
回答 2查看 11.1K关注 0票数 16

我想知道如何使用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');

任何帮助都将不胜感激!

EN

回答 2

Stack Overflow用户

发布于 2009-07-31 21:44:16

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

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

function AlertUserOfChange()
{
    Alert(“changed me”);
}
票数 6
EN

Stack Overflow用户

发布于 2009-07-31 21:51:38

这个怎么样?

Y.Event.simulate('#mynode', 'change');
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1215012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档