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

发布于 2024-07-30 01:27:58 字数 1131 浏览 12 评论 0原文

我想知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春风十里 2024-08-06 01:27:58

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

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

function AlertUserOfChange()
{
    Alert(“changed me”);
}

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.

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

function AlertUserOfChange()
{
    Alert(“changed me”);
}
放手` 2024-08-06 01:27:58

这个怎么样

Y.Event.simulate('#mynode', 'change');

How about this

Y.Event.simulate('#mynode', 'change');
遥远的绿洲 2024-08-06 01:27:58

正如您所发现的,YUI 3.0 不支持模拟 change 事件。 不过 YUI 3.1 将支持它。 现在在主干中。

您的第三次尝试:

Y.get('#mynode').simulate('change');

应该在 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:

Y.get('#mynode').simulate('change');

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文