Howto: div with onclick inside another div with onclick javascript

前端 未结 11 2033
刺人心
刺人心 2020-11-28 06:03

just a quick question. I\'m having a problem with divs with onclick javascript within each other. When I click on the inner div it should only fire it\'s onclick javascript,

11条回答
  •  生来不讨喜
    2020-11-28 06:37

    Basically there are two event models in javascript. Event capturing and Event bubbling. In event bubbling, if you click on inside div, the inside div click event fired first and then the outer div click fired. while in event capturing, first the outer div event fired and than the inner div event fired. To stop event propagation, use this code in your click method.

       if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
    

提交回复
热议问题