How to select a span with multiple classes and placed inside an anchor?

一笑奈何 提交于 2019-12-02 15:21:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!