简体   繁体   中英

How do you attach click events to ExtJS template elements?

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

This has 2 main advantages:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM