大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
特性说明和原理图:
我们提供的服务有:网站设计、成都做网站、微信公众号开发、网站优化、网站认证、逊克ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的逊克网站制作公司
示例代码(ie8-示例不提供)
html代码
层级关系: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] + ']' + '
') var timeIndex = window.setTimeout(function(){ $dom.css({ 'borderColor': color, 'borderWidth': '4px' }); }, time); } //捕获 $body[0].addEventListener('click',function(event){ $log.html($log.html() + "-------------------
"); 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 )); }); });
效果图
应用场景
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持创新互联!