是否可以将事件绑定(bind)到 Closure 中类的所有元素?我知道在 jQuery 中它是这样的
$('.my_class').click(...)
Closure中有类似的东西吗?类似的东西
goog.events.listen('.my_class', ...)
最佳答案
你的意思是像下面这样吗?
引用:http://closure-library.googlecode.com/git-history/docs/namespace_goog_events.html
var source = new goog.events.EventTarget();
function handleEvent(e) {
alert('Type: ' + e.type + '; Target: ' + e.target);
}
source.listen('foo', handleEvent);
// Or: goog.events.listen(source, 'foo', handleEvent);
...
source.dispatchEvent('foo'); // will call handleEvent
...
source.unlisten('foo', handleEvent);
// Or: goog.events.unlisten(source, 'foo', handleEvent);
关于JavaScript Closure - 监听类中所有元素的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31867666/