admin管理员组

文章数量:1296877

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'}
]);

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'}
]);
Share asked Jan 9, 2012 at 19:41 Mike ThomsenMike Thomsen 37.5k11 gold badges63 silver badges86 bronze badges 1
  • Can you provide more code? It depends on control. – Krzysztof Commented Jan 9, 2012 at 20:30
Add a ment  | 

1 Answer 1

Reset to default 7

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'});

This has 2 main advantages:

  1. You only need to bind a single listener
  2. It will work as you dynamically modify the content

本文标签: javascriptHow do you attach click events to ExtJS template elementsStack Overflow