最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trigger event on parent click but not on children - Stack Overflow

programmeradmin5浏览0评论

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

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

Share Improve this question asked Jun 20, 2012 at 16:17 Christian BenincasaChristian Benincasa 1,2151 gold badge21 silver badges45 bronze badges
Add a ment  | 

4 Answers 4

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

DEMO: http://jsfiddle/Bt5HA/4/

Access child elements and return false when they're clicked http://jsfiddle/Bt5HA/3/

Change to:

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

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/VnHGh/24/

发布评论

评论列表(0)

  1. 暂无评论