您好,登錄后才能下訂單哦!
特性說明和原理圖:
示例代碼(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>
層級關系:body->wrap->cont->button,可以對照上面的原理
Js代碼
$(function(){ var $log = $('.log'), $wrap = $('.wrap'), $cont = $('.cont'), $btn = document.getElementById('btn'), $stopType = $('#stopType'), $body = $('body'), $inTxt = $('#inTxt'), $btnReject = $('#btnReject'); var ePhase = ["","捕獲","目標","冒泡"] 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); //阻止默認事件 $inTxt.keypress(function(event){ //event.preventDefault(); window.event.returnValue = false; $body.append( String.fromCharCode( event.keyCode )); }); });
效果圖
應用場景
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。