Detecting click event on padding only

前端 未结 3 1808
温柔的废话
温柔的废话 2020-12-11 11:01

I have an HTML element with some padding. I would like to detect for clicks on that element\'s padding. That is, I don\'t want the event to fire when the user clicks on the

3条回答
  •  孤街浪徒
    2020-12-11 11:38

    I think this is what ThiefMaster intended to describe. In this scenario, a click on the content will do nothing but a click on the div with lots of padding will yield an action.

    Basic markup:

    Content content content

    then

    click listener for content div that prevents bubbling to divWithPadding:

    $("#divWithPadding > div").click(function(e){
       e.stopPropagation(); 
    });
    

    click listener for divWithPadding that does something:

    $("#divWithPadding").click(function(){
        //do something
    });
    

提交回复
热议问题