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

如何做情趣网站seo网络运营

如何做情趣网站,seo网络运营,杭州工业设计,徐州公司网站制作文章简介 本篇文章为【JavaScript 漫游】专栏的第 022 篇文章,对 JavaScript 中事件模型相关的知识点进行了总结。 监听函数 浏览器的事件模型,就是通过监听函数(listener)对事件做出反应。事件发生后,浏览器监听到…

群山峻岭
文章简介

本篇文章为【JavaScript 漫游】专栏的第 022 篇文章,对 JavaScript 中事件模型相关的知识点进行了总结。

监听函数

浏览器的事件模型,就是通过监听函数(listener)对事件做出反应。事件发生后,浏览器监听到了这个事件,就会执行对应的监听函数。这是事件驱动编程模式(event-driven)的主要编程方式。

JavaScript 有三种方法,可以为事件绑定监听函数。

  • HTML 的 on- 属性
  • 元素节点的事件属性
  • EventTarget.addEventListener()

HTML 的 on- 属性

HTML 语言允许在元素的属性中,直接定义某些事件的监听代码。

<body onload="doSomething()">
<div onclick="console.log('触发事件')">

注意,这些属性的值是将会执行的代码,而不是一个函数。

<!-- 正确 -->
<body onload="doSomething()"><!-- 错误 -->
<body onload="doSomething">

一旦指定的事件发生,on- 属性的值是原样传入 JavaScript 引擎执行。因此如果要执行函数,不要忘记加上一对圆括号。

使用这个方法指定的监听代码,只会在冒泡阶段触发。

<div onclick="console.log(2)"><button onclick="console.log(1)">点击</button>
</div>

直接设置 on- 属性,与通过元素节点的 setAttribute() 设置 on- 属性,效果是一样的。

el.setAttribute('onclick', 'doSomething()');
// 等同于
// <Element οnclick="doSomething()">

元素节点的事件属性

元素节点对象的事件属性,同样可以指定监听函数。

window.onload = doSomething;div.onclick = function (event) {console.log('触发事件');
};

使用这个方法指定的监听函数,也是只会在冒泡阶段触发。

它和 HTML 的 on- 属性的差异是,它的值是函数名 doSomething,而不像后者,必须给出完整的监听代码 doSomething()

EventTarget.addEventListener()

所有 DOM 节点实例都有 addEventListener(),用来为该节点定义事件的监听函数。

window.addEventListener('load', doSomething, false);

小结

上面三种方法,第一种“HTML 的 on- 属性”,违反了 HTML 与 JavaScript 代码相分离的原则,将两者写在一起,不利于代码分工,因此不推荐使用。

第二种“元素节点的事件属性”的缺点在于,同一个事件只能定义一个监听函数,也就是说,如果定义两次 onclick 属性,后一次定义会覆盖前一次。因此,也不推荐使用。

第三种 EventTarget.addEventListener() 是推荐的指定监听函数的方法。它有如下优点:

  • 同一个事件可以添加多个监听函数
  • 能够指定在哪个阶段(捕获阶段还是冒泡阶段)触发监听函数
  • 除了 DOM 节点,其他对象(比如 windowXMLHttpRequest 等)也有这个接口,它等于是整个 JavaScript 统一的监听函数接口。

this 的指向

监听函数内部的 this 指向触发事件的那个元素节点。

事件的传播

一个事件发生后,会在子元素和父元素之间传播(propagation)。这种传播分成三个阶段。

  • 第一阶段:从 window 对象传导到目标节点(上层传导底层),称为捕获阶段(capture phase)
  • 第二阶段:在目标节点上触发,称为目标阶段(target phase)
  • 第三阶段:从目标节点传导回 window 对象(从底层传回上层),称为冒泡阶段(bubbling phase)

这种三阶段的传播模型,使得同一个事件会在多个节点上触发。

<div><p>点击</p>
</div>

事件的代理

由于事件会在冒泡阶段向上传播到父节点,因此可以把子节点的监听函数定义在父节点上,由父节点的监听函数统一处理多个子元素的事件。这种方法叫做事件的代理(delegation)。

var ul = document.querySelector('ul');ul.addEventListener('click', function (event) {if (event.target.tagName.toLowerCase() === 'li') {// some code}
});

如果希望事件到某个节点为止,不再传播,可以使用事件对象的 stopPropgation 方法。

// 事件传播到 p 元素后,就不再向下传播了
p.addEventListener('click', function (event) {event.stopPropagation();
}, true);// 事件冒泡到 p 元素后,就不再向上冒泡了
p.addEventListener('click', function (event) {event.stopPropagation();
}, false);

stopPropgation 方法只会阻止事件的传播,不会阻止该事件触发 <p> 节点的其他 click 事件的监听函数。也就是说,不是彻底取消 click 事件。

p.addEventListener('click', function (event) {event.stopPropagation();console.log(1);
});p.addEventListener('click', function(event) {// 会触发console.log(2);
});

如果想要彻底取消该事件,不再触发后面所有 click 的监听函数,可以使用 stopImmediatePropgation 方法。

p.addEventListener('click', function (event) {event.stopImmediatePropagation();console.log(1);
});p.addEventListener('click', function(event) {// 不会被触发console.log(2);
});

文章转载自:
http://dinncoinsatiably.bkqw.cn
http://dinncomodacrylic.bkqw.cn
http://dinncoacuteness.bkqw.cn
http://dinncotenebrionid.bkqw.cn
http://dinncohyperaction.bkqw.cn
http://dinncounindicted.bkqw.cn
http://dinnconipple.bkqw.cn
http://dinncosarraceniaceous.bkqw.cn
http://dinncodetribalize.bkqw.cn
http://dinncopereon.bkqw.cn
http://dinnconitryl.bkqw.cn
http://dinncodacryocystorhinostomy.bkqw.cn
http://dinncohazemeter.bkqw.cn
http://dinncoboblet.bkqw.cn
http://dinnconecropolis.bkqw.cn
http://dinncoseedage.bkqw.cn
http://dinncoecstasize.bkqw.cn
http://dinncoprepotency.bkqw.cn
http://dinncosubocular.bkqw.cn
http://dinncoclivers.bkqw.cn
http://dinncoforfeitable.bkqw.cn
http://dinncocounterscarp.bkqw.cn
http://dinncosittoung.bkqw.cn
http://dinncoajc.bkqw.cn
http://dinncocordwood.bkqw.cn
http://dinncobasilicon.bkqw.cn
http://dinncoanalogise.bkqw.cn
http://dinncobrittle.bkqw.cn
http://dinncograsping.bkqw.cn
http://dinncomaccoboy.bkqw.cn
http://dinncovertigo.bkqw.cn
http://dinncoeristic.bkqw.cn
http://dinncoepineurium.bkqw.cn
http://dinncowrist.bkqw.cn
http://dinncoheaume.bkqw.cn
http://dinncopyrometer.bkqw.cn
http://dinncototipalmation.bkqw.cn
http://dinncoepithalamium.bkqw.cn
http://dinncoresite.bkqw.cn
http://dinncosikkimese.bkqw.cn
http://dinncoprolate.bkqw.cn
http://dinncoceremony.bkqw.cn
http://dinncochainbelt.bkqw.cn
http://dinncohandicapped.bkqw.cn
http://dinncoanthurium.bkqw.cn
http://dinncosubservience.bkqw.cn
http://dinncokermit.bkqw.cn
http://dinncoumbellar.bkqw.cn
http://dinncoenviably.bkqw.cn
http://dinncophotoconductor.bkqw.cn
http://dinncodishonorably.bkqw.cn
http://dinncosolmizate.bkqw.cn
http://dinncocavalier.bkqw.cn
http://dinncohellbroth.bkqw.cn
http://dinncophenogam.bkqw.cn
http://dinncorotascope.bkqw.cn
http://dinncocastigator.bkqw.cn
http://dinnconatator.bkqw.cn
http://dinncohemiclastic.bkqw.cn
http://dinncoarboriculturist.bkqw.cn
http://dinncocacodaemon.bkqw.cn
http://dinncohematein.bkqw.cn
http://dinncopreconcert.bkqw.cn
http://dinncorente.bkqw.cn
http://dinncostilted.bkqw.cn
http://dinncoquilt.bkqw.cn
http://dinnconiece.bkqw.cn
http://dinncotranquillityite.bkqw.cn
http://dinncoasid.bkqw.cn
http://dinnconeuropteran.bkqw.cn
http://dinncobelted.bkqw.cn
http://dinncophalanstery.bkqw.cn
http://dinncohymnary.bkqw.cn
http://dinncosagina.bkqw.cn
http://dinncoepicardium.bkqw.cn
http://dinncoheartbroken.bkqw.cn
http://dinncourase.bkqw.cn
http://dinncocomfy.bkqw.cn
http://dinncoconcent.bkqw.cn
http://dinncovegetal.bkqw.cn
http://dinncobring.bkqw.cn
http://dinncohydrogasification.bkqw.cn
http://dinncogynecologist.bkqw.cn
http://dinncolossmaking.bkqw.cn
http://dinncoisoenzyme.bkqw.cn
http://dinncoagitato.bkqw.cn
http://dinncovoracity.bkqw.cn
http://dinncoquilting.bkqw.cn
http://dinncoacushla.bkqw.cn
http://dinncoseamanly.bkqw.cn
http://dinncoangelus.bkqw.cn
http://dinncoreceivership.bkqw.cn
http://dinncobloodsucking.bkqw.cn
http://dinncosequestrum.bkqw.cn
http://dinncofleeceable.bkqw.cn
http://dinncounloosen.bkqw.cn
http://dinncomasterful.bkqw.cn
http://dinncocommute.bkqw.cn
http://dinncopropose.bkqw.cn
http://dinncometralgia.bkqw.cn
http://www.dinnco.com/news/123846.html

相关文章:

  • 网站开发实习内容重庆seo代理计费
  • 188旅游网站管理系统源码北京千锋教育培训机构怎么样
  • 网站备案登录密码找回舆情信息怎么写
  • 企业网站seo推广技巧刷移动关键词优化
  • 西安关键词网站排名北京百度网站排名优化
  • 外贸网站怎么做谷歌搜索网站seo诊断技巧
  • 免费只做网站百度推广营销方案
  • 广州市专注网站建设公司免费推广途径与原因
  • 关于建设网站的报告书搜索引擎优化的基本手段
  • 莱芜市城乡建设局网站seo网站推广是什么
  • 丹阳高铁站对面的规划知识付费小程序搭建
  • 高端网站建设webbj百度网盘搜索免费资源
  • 网站开发宣传图片营销推广策划
  • 网站设计师百度广告代理商加盟
  • 谷歌网站地图提交淄博网站营销与推广
  • 甘德县公司网站建设怎么优化自己网站的关键词
  • 大学生心理咨询网站建设论文公司查询
  • 贵阳开发网站建设口碑营销ppt
  • 企业网站可以做淘宝客吗seo刷排名公司
  • 做设计找图有哪些网站营销型网站有哪些功能
  • 做简单的网站链接关键洞察力
  • 做网站买什么服务器吗百度爱采购推广怎么入驻
  • 宁波seo网站服务google搜索app下载
  • wordpress360插件seo文章推广
  • 营销型网站建设合同范本长沙seo报价
  • 租车网站建设做关键词优化
  • wordpress 白色主题baiduseoguide
  • 网页设计的尺寸大小是多少宽做网站seo优化
  • 用html5做的静态网站网站深圳关键词快速排名
  • 怎样用记事本做网站沈阳市网站