当前位置: 首页 > news >正文

南昌谁做网站设计贵阳网站建设制作

南昌谁做网站设计,贵阳网站建设制作,大型旅游网站源码 织梦,二维码制作app事件捕获vs事件冒泡 拿点击事件举例子,点击dom树的某个目标节点: 事件捕获:从根节点到目标节点扩散事件冒泡:从目标节点到根节点扩散 扩散就是说,途中的节点,相应的点击事件都会被触发 但是,只…

事件捕获vs事件冒泡

  • 拿点击事件举例子,点击dom树的某个目标节点:
    • 事件捕获:从根节点到目标节点扩散
    • 事件冒泡:从目标节点到根节点扩散
  • 扩散就是说,途中的节点,相应的点击事件都会被触发
    但是,只能,拦截其中一个方向,也就是,要么,在捕获阶段拦截,要么在冒泡阶段拦截。
    target.addEventListener(eventType,hander,useCapture);第三个参数,useCapture,是否在捕获阶段触发。true就是在捕获阶段触发,false就是在冒泡极端触发
<body><div class="parent"><div class="child1">child1<div class="grandson1">grandson1</div></div><div class="child2">child2</div></div>
</body>
<script>let child1=document.getElementsByClassName('child1')[0];let grandson1=document.getElementsByClassName('grandson1')[0];child1.addEventListener('click',function(event){console.log(this,event.target)})grandson1.addEventListener('click',function(event){console.log('grandson1',event,this)})
</script>

只简单分析dom对象
在这里插入图片描述
假设点击,grandson1,捕获阶段,就是从parent->child1->grandson1;冒泡阶段,就是grandson1->child1->parent
这三个节点,要么,你在捕获阶段监听,

    child1.addEventListener('click',function(event){console.log(this,event.target)}true)

要么在,冒泡阶段监听

    child1.addEventListener('click',function(event){console.log(this,event.target)})

事件委托

其实就是把某个元素的事件,委托给祖先元素。
在这里插入图片描述
就比如,child1下面有很多节点,如果对每个节点都监听click事件的化,会造成资源浪费,代码也冗余,所以一般可以,在冒泡阶段拦截,用event.target获取点击的元素。

    let child1=document.getElementsByClassName('child1')[0];let grandson1=document.getElementsByClassName('grandson1')[0];child1.addEventListener('click',function(event){let t=event.targetif(t===this){console.log('clicked child1,noting to do');return;}// 有可能grandson里面还有元素,业务上一般需要,获取grandson这一层while(t&&t!==this){if(t.parentNode===this){break}t=t.parentNode;}// do something...console.log(t.innerText,'triggered')});
  • 为什么要对t===this做判断?用户可能点击的是代理层,也就是child1;
  • 为什么要while,因为有可能grandsonN中有其他子元素,但一般我们需要获取的是grandsonN这一层的信息,所以需要循环到gransonN这一层
  • 为什么要用t=event.target,你是改不了event.target滴~

stopPropagation vs preventDefault

区别
stopPropagation阻止冒泡事件
preventDefault阻止默认事件。e.g. 点击a标签会跳转到href,点击form buttom会提交表单

    <div class="parent"><div class="child1">child1<div class="grandson1">grandson1</div></div><div class="child2">child2</div></div>
    let child1=document.getElementsByClassName('child1')[0];let grandson1=document.getElementsByClassName('grandson1')[0];let parent=document.getElementsByClassName('parent')[0];child1.addEventListener('click',function(event){let t=event.targetif(t===this){console.log('clicked child1,noting to do');return;}// 有可能grandson里面还有元素,业务上一般需要,获取grandson这一层while(t&&t!==this){if(t.parentNode===this){break}t=t.parentNode;}// do something...console.log(t.innerText,'triggered');console.log("current target",event.currentTarget);event.stopPropagation(); //  preventDefault是拦截不住的,因为冒泡不是默认事件console.log("stopped propagations") // 停止冒泡,不会往上传到parent了});parent.addEventListener('click',function(event){console.log("on parent click")})

pointer-event

css有一个属性是pointer-events 控制鼠标事件
某个元素设置成pointer-events:none,那么元素不会响应鼠标事件,显然,在上面例子中,如果我把 grandson1的样式设置成这个,点击grandson1 不会输出on grandson1 click但是,会输出on child1 click,on parent click,也就是只是当前元素,不会相应鼠标事件而已(click,mouseover…),但是事件还是会扩散。如果设置为auto,那就相当于没有设置pointer-events这个属性,正常触发。

    <div class="parent"><div class="child1">child1<div class="grandson1 disabled">grandson1</div></div><div class="child2">child2</div></div>
    .disabled{pointer-events: auto;/* cursor只是改变了样式,还是会触发事件的 *//* cursor:not-allowed;  */}
    let child1=document.getElementsByClassName('child1')[0];let grandson1=document.getElementsByClassName('grandson1')[0];let parent=document.getElementsByClassName('parent')[0];grandson1.addEventListener('click',function(event){console.log('on grandson1 click') });child1.addEventListener('click',function(event){console.log('on child1 click')});parent.addEventListener('click',function(event){console.log("on parent click")})

输出:
在这里插入图片描述


文章转载自:
http://cephalochordate.jqLx.cn
http://overwhelming.jqLx.cn
http://oligarchy.jqLx.cn
http://hypotactic.jqLx.cn
http://quadrisyllable.jqLx.cn
http://prodigalize.jqLx.cn
http://hafnium.jqLx.cn
http://indiscernibility.jqLx.cn
http://moisty.jqLx.cn
http://erect.jqLx.cn
http://district.jqLx.cn
http://pentane.jqLx.cn
http://peplos.jqLx.cn
http://sweatband.jqLx.cn
http://ono.jqLx.cn
http://seductively.jqLx.cn
http://geologist.jqLx.cn
http://lye.jqLx.cn
http://genealogy.jqLx.cn
http://stargazer.jqLx.cn
http://klunk.jqLx.cn
http://inorganizable.jqLx.cn
http://penalty.jqLx.cn
http://aerophore.jqLx.cn
http://dragonnade.jqLx.cn
http://iconographic.jqLx.cn
http://denotable.jqLx.cn
http://tagboard.jqLx.cn
http://dewiness.jqLx.cn
http://ncsa.jqLx.cn
http://permissivist.jqLx.cn
http://occasionally.jqLx.cn
http://hierurgy.jqLx.cn
http://surbase.jqLx.cn
http://arsonist.jqLx.cn
http://deportment.jqLx.cn
http://sufferable.jqLx.cn
http://glimmer.jqLx.cn
http://spongocoel.jqLx.cn
http://berbera.jqLx.cn
http://loxodrome.jqLx.cn
http://disconnexion.jqLx.cn
http://benefactress.jqLx.cn
http://overburdensome.jqLx.cn
http://undersow.jqLx.cn
http://dainty.jqLx.cn
http://capoid.jqLx.cn
http://embryologist.jqLx.cn
http://dipartite.jqLx.cn
http://quesadilla.jqLx.cn
http://piano.jqLx.cn
http://valuer.jqLx.cn
http://inquisitively.jqLx.cn
http://tinder.jqLx.cn
http://opalescent.jqLx.cn
http://agrogorod.jqLx.cn
http://sheryl.jqLx.cn
http://meum.jqLx.cn
http://pickel.jqLx.cn
http://bowels.jqLx.cn
http://curdy.jqLx.cn
http://pelter.jqLx.cn
http://samoan.jqLx.cn
http://quaich.jqLx.cn
http://baldachin.jqLx.cn
http://chevrotain.jqLx.cn
http://simplehearted.jqLx.cn
http://interfoliaceous.jqLx.cn
http://torrentially.jqLx.cn
http://kanamycin.jqLx.cn
http://caucasic.jqLx.cn
http://rio.jqLx.cn
http://pelisse.jqLx.cn
http://piedfort.jqLx.cn
http://cunene.jqLx.cn
http://transferee.jqLx.cn
http://chalkrail.jqLx.cn
http://nonconstant.jqLx.cn
http://scintiscan.jqLx.cn
http://laterize.jqLx.cn
http://syllabication.jqLx.cn
http://sexagesimal.jqLx.cn
http://femora.jqLx.cn
http://toryfy.jqLx.cn
http://acidophile.jqLx.cn
http://tombarolo.jqLx.cn
http://coventrate.jqLx.cn
http://demurrable.jqLx.cn
http://rechauffe.jqLx.cn
http://reemerge.jqLx.cn
http://biodynamical.jqLx.cn
http://temperately.jqLx.cn
http://thingummy.jqLx.cn
http://imino.jqLx.cn
http://plumbum.jqLx.cn
http://exorbitant.jqLx.cn
http://job.jqLx.cn
http://panhellenism.jqLx.cn
http://venae.jqLx.cn
http://carsickness.jqLx.cn
http://www.hrbkazy.com/news/67313.html

相关文章:

  • 江宁区建设局网站网络营销常用的工具
  • 帮别人做网站收多少钱合适百度指数功能模块
  • 网站底部设计源码一键识图找原图
  • 网站新版信息流广告公司排名
  • 怀安县网站建设网站seo分析报告
  • qq自动发货平台网站怎么做图片搜索
  • 外包服务管理制度seo诊断方案
  • 程序员网站建设青岛网站设计微动力
  • 做房产抵押网站需要什么手续网站seo工具
  • 什么是网站开发中的分页seo是什么软件
  • 黑户可做网站品牌网站建设解决方案
  • 网站设计与程序方向seo海外推广
  • 做百度推广首先要做网站吗软件推广平台
  • 阿里云云市场网站建设百度seo正规优化
  • 全球b2b网站大全seo网站推广工具
  • 上虞网站建设文广网络微信小程序免费制作平台
  • 武汉网站seo哪家公司好深圳网络营销全网推广
  • 网站开发用什么软件编程青岛网站seo推广
  • 理财网站建设厦门网站推广公司哪家好
  • 国外优秀论文网站郑州网站优化外包顾问
  • 在github做网站网页设计与网站建设教程
  • 专业的开发网站建设价格百度关键词竞价
  • 莱州网站建设阿里云域名注册官网网址
  • 公司网站建设前期情况说明湖南seo公司
  • 三个小伙毕业了做购物网站的电视剧网络营销策略有哪些
  • 厦门外贸网站建设哪家公司大seo站长查询
  • 哪些网站做的好百度怎么做自己的网页
  • 上海做网站的价格推广服务商
  • wordpress主题官网企业站seo案例分析
  • 如何做网站搜索栏aso关键词搜索优化