Child element click event trigger the parent click event

前端 未结 5 1470
轮回少年
轮回少年 2020-11-28 09:24

Say you have some code like this:





   
5条回答
  •  北海茫月
    2020-11-28 09:48

    I faced the same problem and solve it by this method. html :

    AAA
    BBBB

    JS:

    $(document).ready(function(){
     $("#parentDiv").click(function(e){
       if(e.target.id=="childDiv"){
         childEvent();
       } else {
         parentEvent();
       }
     });
    });
    
    function childEvent(){
        alert("child event");
    }
    
    function parentEvent(){
        alert("paren event");
    }
    

提交回复
热议问题