问题
I'm in a situation where I need to bind a click event to a span (placed inside an achor having multiple classes) based on the classes applied to it.
For eg:
UPDATE: Added the html eg below
<div class="c0 c">
<a class="c1 c2">
<span class="c3 c4"></span>Anchor_Text
</a>
</div>
Now I'm binding a click event as below:
$('span.c3.c4').click(function (e) { alert("clicked!"); });
The above jQuery code doesnt work. However if I use the classes on anchor to bind the click event, it works. Please see below:
$(.c1').click(function (e) { alert("clicked!"); });
$(.c2').click(function (e) { alert("clicked!"); });
Any help will be appreciated!
UPDATED: Please find the html example updated within the Q now.
回答1:
Multiple classes should be selected like this:
$('span.c3.c4').parents('a').click(function (e) { alert("clicked!"); });
See a working demo here > http://jsfiddle.net/JeG3A/
来源:https://stackoverflow.com/questions/14506319/how-to-select-a-span-with-multiple-classes-and-placed-inside-an-anchor