admin管理员组文章数量:1429617
Issue: YUI3 - Onclick event handling for links having same classes
We have few links in the page having same class. When I click on one of the links there are some different actions to be taken based on the which link was clicked, For e.g.
<a href="?page=1" data="test1" class="sample">One</a>
<a href="?page=2" data="test2" class="sample">two</a>
<a href="?page=3" data="test3" class="sample">three</a>
handler code:
Y.all('.sample').on('click', function(e){
e.preventDefault();
alert(this.getAttribute("data"));
});
when I click any of the links I get the list of all "data" attribute. We only need the data of link clicked.
Issue: YUI3 - Onclick event handling for links having same classes
We have few links in the page having same class. When I click on one of the links there are some different actions to be taken based on the which link was clicked, For e.g.
<a href="?page=1" data="test1" class="sample">One</a>
<a href="?page=2" data="test2" class="sample">two</a>
<a href="?page=3" data="test3" class="sample">three</a>
handler code:
Y.all('.sample').on('click', function(e){
e.preventDefault();
alert(this.getAttribute("data"));
});
when I click any of the links I get the list of all "data" attribute. We only need the data of link clicked.
Share Improve this question edited Aug 1, 2012 at 15:27 Neo asked Aug 1, 2012 at 14:52 NeoNeo 5,22810 gold badges47 silver badges66 bronze badges3 Answers
Reset to default 3You can also use e.target
instead of this
to access the element being clicked:
Y.all('.sample').on('click', function(e){
e.preventDefault();
alert(e.target.getAttribute("data"));
});
And for even better performance you can use event delegation:
Y.one('body').delegate('click', function(e){
e.preventDefault();
alert(this.getAttribute("data"));
}, '.sample');
all
gives you a list of matched elements. You can use each
to iterate through the list and do stuff for individual node. Refer to their Node Class API for more information.
Here is the code, and an example on jsfiddle.
Y.all('.sample').each(function(node) {
node.on('click', function(e) {
e.preventDefault();
alert(node.getAttribute("data"));
});
});
There is also a FAQ entry for this question in the Event user guide on yuilibrary. (http://yuilibrary./yui/docs/event/#nodelist-this). It's generally preferable to use event delegation.
Here event delegation is better solution, agree with @juandopazo
If you do not want to use delegation, you can use Y.all('.sample').each(...) (solution from @user322896) or e.target (solution from @juandopazo), but I usually do it another way:
Y.on('click', function(e){
e.preventDefault();
alert(this.getAttribute("data"));
}, '.sample');
This is not a delegation syntax (listeners will be attached on links directelly) and there is no NodeList object, so this
refers to particular link node.
This way is also have preformance advantage paring to Y.all('.sample')
, read this: Why would I use Y.on() or Y.delegate() instead of node.on() and node.delegate()?
本文标签: javascriptYUI3Onclick event handling for links having same classesStack Overflow
版权声明:本文标题:javascript - YUI3 - Onclick event handling for links having same classes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745535588a2662256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论