<div id=div1 onclick="alert(33)" style="width:200px;height:50px;background-color:#123456;">
<div id=idv2 onclick="alert(44)" style="width:100px;height:100px;background-color: #654321;"></div>
</div>现在点击div2 div1也会触发,我想要的是点击div2 div1不会被触发,  div1必须包含div2,求解决办法

解决方案 »

  1.   

    <div id=div1 onclick="alert(33)" style="width:200px;height:50px;background-color:#123456;">
            <div id=idv2 onclick="alert(44);event.cancelBubble=true;if(event.preventDefault)event.preventDefault()" style="width:100px;height:100px;background-color: #654321;"></div>
        </div>
      

  2.   

    $('#div2').click(function(e) {
        e.stopPropagation();
    });
      

  3.   


    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/JavaScript">
    $(function(){
    $("#idv2").click(function(event){
    event.stopPropagation(); 
    alert(44);
    });
    /*$("#idv2").click(function(){//这种方法也可以
    alert(44);
    return false;
    })*/;
    $("#div1").click(function(){
    alert(33);
    });
    })
    </script>
    <div id=div1 style="width:200px;height:50px;background-color:#123456;">
    <div id=idv2 style="width:100px;height:100px;background-color: #654321;"></div>
    </div>