国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

關(guān)于javascript冒泡與默認(rèn)事件的使用詳解

2020-01-24 03:19:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

對(duì)于javascript的冒泡,我一直誤解它了,

冒泡,即是從底層往外blow blow blow ...

慚愧的是,我一直以為阻止冒泡是阻止父元素往子元素傳遞事件……

貼上一串代碼以便往后回顧!

復(fù)制代碼 代碼如下:

<script type="text/javascript">
 window.onload=function(){
 var a=document.getElementById("a");
 var b=document.getElementById("b");
 var c=document.getElementById("c");
 var c=document.getElementById("d");

     a.onclick=function(e){
         this.style.background="#000";   
         };

     b.onclick=function(e){
             this.style.background="#ccc";
             //阻止事件冒泡
             window.event.cancelBubble = true;//IE8以下
             e.stopPropagation();
         };

     d.onmousedown=function(e){
         //阻止默認(rèn)事件,比如在chrome下圖片有拖拽默認(rèn)行為
         window.event.returnValue = false;
         e.preventDefault();
     }
 }

 

 </script>

復(fù)制代碼 代碼如下:

Html部分
 <div id="a" style="width:300px;height:300px;background:red;overflow:hidden;">
     <div id="b" style="width:200px;height:200px;background:green;margin:50px 0 0 50px;overflow:hidden;">
         <div id="c" style="width:100px;height:100px;background:yellow;margin:50px 0 0 50px;overflow:hidden;">
             <img src="240x240.jpg" width="50" height="50" id="d" />
         </div>
     </div>
 </div>

另一個(gè)例子:
復(fù)制代碼 代碼如下:

<script type="text/javascript">
window.onload=function(){
    document.getElementById("test").addEventListener('click',function(e){
        alert('aaaa')
    },false);

    document.getElementById("test1").addEventListener('click',function(e){
        alert('bbb')
        e.stopPropagation();
    },false)
}
</script>

復(fù)制代碼 代碼如下:

<style type="text/css">
#test1{width:100px;height:100px;background:#ff0}
#test2{width:100px;height:100px;background:#ff0}
</style>
<div id="test" style="width:100px;height:100px;background:#f60;padding:20px;">
    <div id="test1"></div>
</div>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高密市| 浪卡子县| 湛江市| 四会市| 长葛市| 奉贤区| 涿鹿县| 肃北| 苗栗县| 大兴区| 安吉县| 通许县| 山丹县| 名山县| 共和县| 泗洪县| 右玉县| 固阳县| 宜昌市| 朝阳区| 贺兰县| 定南县| 汝城县| 怀仁县| 红河县| 东安县| 咸阳市| 扬中市| 临汾市| 尚志市| 封丘县| 永城市| 绥德县| 那坡县| 靖宇县| 拜泉县| 达拉特旗| 连山| 雷山县| 淮滨县| 长丰县|