Howto: div with onclick inside another div with onclick javascript

前端 未结 11 1986
刺人心
刺人心 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:43

    One more way for webkit based browsers:

    <div onclick="alert('inner'); event.stopPropagation;" ...
    
    0 讨论(0)
  • 2020-11-28 06:49

    Just add this code :

    window.event.stopPropagation();
    
    0 讨论(0)
  • 2020-11-28 06:49

    you have two 'div' and three '/div'.

    0 讨论(0)
  • 2020-11-28 06:49

    This worked for me in Jquery:

    $('#outer_element').click(function(event) {
        if(event.target !== event.currentTarget) return;
        alert("Outer element is clicked")                       
    });
    

    This way, if the current target is not the same as the outer div, it will do nothing. You can implement normal functions on the child elements.

    0 讨论(0)
  • 2020-11-28 06:50

    Check out the info on event propagation here

    In particular you'll want some code like this in your event handlers to stop events from propagating:

    function myClickHandler(e)
    {
        // Here you'll do whatever you want to happen when they click
    
        // now this part stops the click from propagating
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
    }
    
    0 讨论(0)
提交回复
热议问题