How to select a span with multiple classes and pla

2020-05-04 14:51发布

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条回答
姐就是有狂的资本
2楼-- · 2020-05-04 15:22

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/

查看更多
登录 后发表回答