Developer Test-Jquery-阻止冒泡

來源:互聯網
上載者:User

標籤:toc   turn   方法   attr   isp   blank   www   lib   跳轉   

今天來看看前端的冒泡和事件預設事件如何處理

1.event.stopPropagation()方法

這是阻止事件的冒泡方法,不讓事件向documen上蔓延,但是預設事件任然會執行,當你掉用這個方法的時候,如果點擊一個串連,這個串連仍然會被開啟,

2.event.preventDefault()方法

這是阻止預設事件的方法,調用此方法是,串連不會被開啟,但是會發生冒泡,冒泡會傳遞到上一層的父元素;

3.return false  ;

這個方法比較暴力,他會同事阻止事件冒泡也會阻止預設事件;寫上此代碼,串連不會被開啟,事件也不會傳遞到上一層的父元素;可以理解為return false就等於同時調用了event.stopPropagation()和event.preventDefault()

4.我們來看看幾組demo,使用jQuery進行DOM操作

這是html代碼,div裡面套了一個a標籤,串連到百度

 

[html] view plain copy  print?
  1. <div class="box1">  
  2.             <a href="http://www.baidu.com" target="_blank"></a>  
  3.         </div>  

css代碼,a標籤占父元素的空間的1/4,背景顏色為紅色;

 

 

[html] view plain copy  print?
  1. .box1{  
  2.                 height: 200px;  
  3.                 width: 600px;  
  4.                 margin: 0 auto;  
  5.             }  
  6.             .box1 a{  
  7.                 display: block;  
  8.                 height: 50%;  
  9.                 width: 50%;  
  10.                 background:red;  
  11.             }  

 

 

 

下面來看js代碼,第一種 

 

[html] view plain copy  print?
  1. //不阻止事件冒泡和預設事件  
  2.               
  3.             $(".box1").click(function(){  
  4.                 console.log("1")//不阻止事件冒泡會列印1,頁面跳轉;               
  5.             });  



 

 

第二種

 

[html] view plain copy  print?
  1. //阻止冒泡  
  2.             $(".box1 a").click(function(event){  
  3.                 event.stopPropagation();//不會列印1,但是頁面會跳轉;              
  4.   
  5.             });  
  6.               
  7.             $(".box1").click(function(){  
  8.                 console.log("1")                  
  9. });  



 

 

 



第三種

 

 

[html] view plain copy  print?
  1. $(".box1 a").click(function(event){           
  2. //阻止預設事件  
  3. event.preventDefault();//頁面不會跳轉,但是會列印出1,  
  4. });  
  5.               
  6. $(".box1").click(function(){  
  7. console.log("1");                 
  8. });  



 

 

 


第四種

 

 

[html] view plain copy  print?
  1. $(".box1").click(function(){  
  2. console.log("1")  
  3. });           
  4. //阻止冒泡  
  5. $(".box1 a").click(function(event){  
  6. event.stopPropagation();              
  7. //阻止預設事件  
  8. event.preventDefault() //頁面不會跳轉,也不會列印出1  
  9. })  



 

 

第五種

 

[html] view plain copy  print?
    1. $(".box1").click(function(){  
    2.                 console.log("1")                  
    3.             });                                   
    4. $(".box1 a").click(function(event){  
    5.                 return false;  //頁面不會跳轉,也不會列印出1,等於同時調用了event.stopPropagation()和event.preventDefault()  
    6.   
    7. });  

Developer Test-Jquery-阻止冒泡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.