How to determine if child was clicked in Jquery

后端 未结 6 892
广开言路
广开言路 2021-02-07 07:33

Is there a ways in jQuery to tell which child was clicked if the onclick event was binded to the parent?

For instance:

  $(\'#daddy\').click(function ()          


        
6条回答
  •  深忆病人
    2021-02-07 08:00

    use event.target to get the really clicked node:

    $('#daddy span').on('click',function(event){
        if(event.target.nodeName=='BUTTON'){//Was clicked a button
            ....
        }
        if(event.target.id=='id'){//Was clicked #id
            ....
        }
        ...
    });
    

提交回复
热议问题