Two differents OnClick on two divs, one over the other

后端 未结 6 1561
心在旅途
心在旅途 2020-12-06 06:50

I have two divs, a big one and a smaller over the other, each div has its own OnClick method. The problem I have is when I clicked the smaller div, the big div\'s OnClick me

6条回答
  •  半阙折子戏
    2020-12-06 07:23

    What you're dealing with is event bubbling. Take a look at this article: http://www.quirksmode.org/js/events_order.html.

    Basically, to stop the event from passing to the parent element, you can use something like this:

    document.getElementById('foo').onClick = function(e) {
    
        // Do your stuff
    
        // A cross browser compatible way to stop propagation of the event:
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
    }
    

提交回复
热议问题