您好,登錄后才能下訂單哦!
特性說(shuō)明和原理圖:
示例代碼(ie8-示例不提供)
html代碼
<body class="body" > <div class="log"></div> <input type="text" id="inTxt" name="intxt" /> <div class="wrap"> <div class="cont"> <button type="button" class="button" id="btn">按鈕</button> <select name="stopType" id="stopType"> <option value="1">StopPropagation</option> <option value="2">cancelBubble</option> </select> <button type="button" class="button" id="btnReject">cont阻止捕獲或冒泡</button> </div> </div> </body>
層級(jí)關(guān)系:body->wrap->cont->button,可以對(duì)照上面的原理
Js代碼
$(function(){ var $log = $('.log'), $wrap = $('.wrap'), $cont = $('.cont'), $btn = document.getElementById('btn'), $stopType = $('#stopType'), $body = $('body'), $inTxt = $('#inTxt'), $btnReject = $('#btnReject'); var ePhase = ["","捕獲","目標(biāo)","冒泡"] var setBorderColor = function( $dom, color, time,event){ $dom = $($dom); $log.html($log.html() + $dom.attr('class') + '[' + ePhase[event.eventPhase] + ']' + '<br/>') var timeIndex = window.setTimeout(function(){ $dom.css({ 'borderColor': color, 'borderWidth': '4px' }); }, time); } //捕獲 $body[0].addEventListener('click',function(event){ $log.html($log.html() + "-------------------<br>"); setBorderColor($body,'#0866ff ',0,event); },true); $wrap[0].addEventListener('click',function(event){ setBorderColor($wrap,'yellow',2000,event); },true); $cont[0].addEventListener('click',function(event){ event = event || window.event; if( $stopType.val() == '1' ){ event.stopPropagation(); }else{ event.cancelBubble = true; } setBorderColor($cont,'green',1000,event); },true); $btn.addEventListener('click', function(event){ setBorderColor($btn,'red',0,event); },true); $btnReject[0].addEventListener('click',function(event){ setBorderColor($btnReject,'gray ',0,event); },true); //冒泡 $body[0].addEventListener('click',function(event){ setBorderColor($body,'#0866ff ',0,event); },false); $wrap[0].addEventListener('click',function(event){ setBorderColor($wrap,'yellow',2000,event); },false); $cont[0].addEventListener('click',function(event){ setBorderColor($cont,'green',1000,event); },false); $btn.addEventListener('click', function(event){ setBorderColor($btn,'red',0,event); },false); $btnReject[0].addEventListener('click',function(event){ setBorderColor($btnReject,'gray ',0,event); },false); //阻止默認(rèn)事件 $inTxt.keypress(function(event){ //event.preventDefault(); window.event.returnValue = false; $body.append( String.fromCharCode( event.keyCode )); }); });
效果圖
應(yīng)用場(chǎng)景
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。