How to stop propagating event from parent div to child div

前端 未结 5 2024
一向
一向 2021-02-20 01:36

I want to stop propagate event from parent div to child div in my html. Here a simple code what I\'m trying to do:

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-20 02:07

    you need to stop propagating in child,not parent so you should write this:

     $('.listed-category').click(function(event){
        event.stopPropagation();
    });
    

    and if you want to click a tag and stop propagation too,you also need to write this:

     $('a#close').click(function(event){
        event.stopPropagation();
    });
    

    Anyways I will suggest Yuriy's answer. I just want you to know that you should put event.stopPropagation() in childrens(if you don't want event of parent is run),instead of parent.

提交回复
热议问题