1.阻止事件冒泡,使成為捕獲型事件觸發(fā)機(jī)制.
function stopBubble(e) { //如果提供了事件對(duì)象,則這是一個(gè)非IE瀏覽器 if ( e && e.stopPropagation )   //因此它支持W3C的stopPropagation()方法   e.stopPropagation(); else  //否則,我們需要使用IE的方式來(lái)取消事件冒泡   window.event.cancelBubble = true; }2.當(dāng)按鍵后,不希望按鍵繼續(xù)傳遞給如HTML文本框?qū)ο髸r(shí),可以取消返回值.即停止默認(rèn)事件默認(rèn)行為.
 //阻止瀏覽器的默認(rèn)行為 function stopDefault( e ) {   //阻止默認(rèn)瀏覽器動(dòng)作(W3C)   if ( e && e.preventDefault )     e.preventDefault();   //IE中阻止函數(shù)器默認(rèn)動(dòng)作的方式   else    window.event.returnValue = false;   return false; }<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><title>效果測(cè)試</title><script language="javascript" type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script><script language="javascript" type="text/javascript">$(document).ready(function(){$('div.c1').click(function(e){alert('單擊了div');});$('div.c2').click(function(e){alert('單擊了div');stopBubble(e);});$(document).click(function(e){alert('單擊了document');});$('#txt1').val('123');$('#txt1').click(function(e){stopBubble(e);});$('#txt1').keydown(function(e){stopDefault(e);alert('你按下了鍵值'+e.keyCode); });})function stopBubble(e) { //如果提供了事件對(duì)象,則這是一個(gè)非IE瀏覽器   if ( e && e.stopPropagation )   //因此它支持W3C的stopPropagation()方法   e.stopPropagation();    else   //否則,我們需要使用IE的方式來(lái)取消事件冒泡   window.event.cancelBubble = true; } //阻止瀏覽器的默認(rèn)行為 function stopDefault( e ) {   //阻止默認(rèn)瀏覽器動(dòng)作(W3C)   if ( e && e.preventDefault )     e.preventDefault();   //IE中阻止函數(shù)器默認(rèn)動(dòng)作的方式   else     window.event.returnValue = false;   return false; }</script><style type="text/css">body{font-size:14px;  }}.c1{  font-family:"Arial Unicode MS"  }.c2{  font-family:helvetica,simsun,arial,clean  }</style></head><body><div class="c1">測(cè)試的文字,這里是樣式C1,單擊以冒泡的形式觸發(fā)事件.</div><hr/><div class="c2">測(cè)試的文字,這里是樣式C2,單擊以捕獲的形式觸發(fā)事件.</div><hr/><div><input id="txt1" name="Text1" type="text" /></div><hr/></body></html>以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持VeVb武林網(wǎng)!
新聞熱點(diǎn)
疑難解答