How to add click event for many buttons JQuery?
I have buttons that has ids like that:
id='button0'
id='button1'
id='button2'
id='button3'
id='button4'
...
I want to add click event for all of them. How can I do it with most performanc开发者_运维技巧e?
simple
you should use class instead !!!
<span class="MySpan"> ...
<span class="MySpan"> ...
<span class="MySpan"> ...
$(".MySpan").click (....
Starts with (^=)
Something like $("button[id^='button']")
Performance wise - not sure what the impact of ^= would be though.
ref: http://api.jquery.com/attribute-starts-with-selector/
$("*[id^='button']").click(
function() {
}
);
精彩评论