如何将单击事件附加到ExtJS模板元素?
问题描述:
除了将onclick=....
内置到XTemplate中之外,我如何向其中的每个链接标记添加click事件?
How would I add a click event to each link tag in this other than by building in onclick=....
into the XTemplate?
new Ext.XTemplate(
'<ul>',
'<tpl for="."><li><a href="#{anchor}">{text}</a></li></tpl>',
'</ul>'
).overwrite('someElement', [
{ text: 'Click me', anchor: '1' },
{ text: 'No, click me', anchor: '2'}
]);
答
最简单的答案是,你没有.相反,您应该使用事件委托:
The short answer is, you don't. Instead, you should use event delegation:
Ext.get('someElement').on('click', function(event, target) {
console.log(target);
}, null, {delegate: 'a'});
这有两个主要优点:
- 您只需要绑定一个侦听器
- 它将在您动态修改内容时起作用