如果单击内部div,则Jquery Onclick div与类和警报

smi*_*ace 0 html javascript jquery class onclick

如果我点击"看到我",它应该提醒我'看到我'.
我可以这样做$('div').点击......它也能正常工作.但我认为最好使用容器div.因为整个页面是一个长页面,如果我使用div进行onclick,那么只要我单击就会运行该函数.

<div class="directFilter">
    <div class="boxWithRightArrow seeMe">See me</div>
    <div class="boldFont filterHeader">Sort By</div>
    <div class="boxWithRightArrow borderTop bestClick">Best click</div>
    <div class="boxWithRightArrow intermediateTime">Intermediate time</div>
    <div class="boxWithRightArrow shortestTime">Shortest time</div>
    <div class="boxWithRightArrow iPreferred">I preferred</div>
</div>


$('.directFilter').click(function() {

        alert($(this).attr('class')); //alerts directFilter 

            if ( $( this ).hasClass( "seeMe" ) ) {
                alert("see me");
            } else if ($(this).hasClass("bestClick")) {
                alert("b click");
            } else if ($(this).hasClass("intermediateTime")) {
                alert("itime");
            } else if ($(this).hasClass("shortestTime")) {
                alert("s time");
            } else if ($(this).hasClass("iPreferred")) {
                alert("i pre");
            }
}
    );
Run Code Online (Sandbox Code Playgroud)

JsFiddle:http://jsfiddle.net/smilyface/e2L7s/

有什么建议?

Adi*_*dil 6

您可以使用event.target来标识事件源.然后,您需要使用HTML()或文本()之类$(event.target).html()$(event.target).text()获取元素的内部内容.

现场演示

$('.directFilter').click(function(event) {
        alert($(event.target).html()); //alerts directFilter 
});
Run Code Online (Sandbox Code Playgroud)

如果您在每个工作中都有独特的工作,那么您需要在条件中使用event.target作为源对象标识.