-2

帮助,解决方案:

http://jsfiddle.net/guin90/mb5BJ/2/

$("#click").on("click", function(){
    if($(this).attr("thesame") == 1){
        // Performs div is clicked on it and not on other elements within it. 
        // It's not working!
    } else {      
        return false;
    }

    alert("Hellow");
    $(this).css("background", "red");

    // Only if you clicked the div # click and not on other elements within 
    // this div. But is not working!? For up when you click on the div 
    // or span element inside the div # click it also performs the function, 
    // it is only if clicked on div # click and not on other elements 
    // within it.

    // and performs other functions that I will put
});

HTML:

<div id="click" thesame="1">

    <!-- Clicks and does not perform function Click() -->
    <span> Title HI! </span>   

    <!-- Clicks and does not perform function Click() -->
    <div id="show_modal"> Show Modal Window </div>

</div>
4

1 回答 1

1

使用event.target来识别点击了哪个元素。像这样的东西应该可以解决问题

$("#click").on("click", function(e){ 
    if (e.target.id != "click" ) return false;
    ....
};

请注意,您需要e作为事件处理程序的参数传递

演示

如果您需要将想法推广到页面中的任何 div,您应该这样做

$("div").on("click", function(e){ 
    if (e.target.nodeName == "DIV") return false;
    ....
};
于 2013-07-29T23:41:24.817 回答