简体   繁体   中英

Trigger event on parent click but not on children

If I have a parent div that is positioned absolutely and then a child div that has a higher z-index and is positioned relatively, is there a way to have a click event register only if the parent div is clicked, but not the inside div?

Relevant jsFiddle

Updated fiddle with text input example

$(".parent").click(function(e) {
    if (e.target == this) {
        $(this).hide();
    }
});​

DEMO: http://jsfiddle.net/Bt5HA/4/

Change to:

$('.child a').click(function(e) {
    $(this).parent('.child').hide();
});​

访问子元素并在点击时返回false http://jsfiddle.net/Bt5HA/3/

Try This

$('#child').click(function(event) {
event.stopPropagation();
alert('You clicked Child');
});


$('#parent').click(function() {
alert('You clicked on Parent');
});

You can check working here http://jsfiddle.net/VnHGh/24/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM