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

济南做网站的网络公司广告平台

济南做网站的网络公司,广告平台,安徽柱石建设有限公司网站,网络管理协议提示:事件捕获,事件冒泡,事件委托 目录 事件模型(DOM事件流) 1.事件是什么 2.事件流 1).事件流的三个阶段 3.参考代码 二、事件委托 1.概念 2.使用案例 3.阻止冒泡行为 事件模型(DOM事件流) 1.事件是什么 1). 事件是HTML和Javascr…

提示:事件捕获,事件冒泡,事件委托

目录

事件模型(DOM事件流)

1.事件是什么

2.事件流

1).事件流的三个阶段

3.参考代码

二、事件委托

1.概念

2.使用案例

3.阻止冒泡行为


事件模型(DOM事件流)

1.事件是什么

1). 事件是HTML和Javascript交互的驱动器, 事件是文档或者浏览器窗口中发生的,特定的交互瞬间。

 2), 事件是用户或浏览器自身执行的某种动作,如 click,load 和 mouseover 都是事件的名字。

3). 事件是 javaScript 和 DOM 之间交互的桥梁。

2.事件流

事件流, 即是一个事件发生的流程或者说流转, 从开始到结束, 都发生了什么

事件发生时会在元素节点之间按照特定的顺序传播,这个传播过程,我们就称之为DOM事件流.

1).事件流的三个阶段

1. 捕获阶段 Capture Phase; 从上到下, 层层传递, 直到目标接收

2. 目标阶段 Target Phase; 确认目标, 进行处理

3. 冒泡阶段 Bubbling Phase; 处理结束, 往上传递.

注意:

1. JS建议执行捕获或者冒泡两个阶段中的其中一个阶段

2. 传统绑定事件方式跟attachEvent绑定事件的方式只能得到冒泡阶段

3. addEventListener(type,listener,[,useCapture])第三个参数如果是true,表示在事件捕获阶段调用事件处理程序;  如果是false(默认不写就是false),表示事件冒泡阶段调用事件处理程序.

 4. 实际开发中我们很少使用事件捕获,我们更关注事件冒泡.

5. 另外需要注意, 不是所有事件都会冒泡的, 有些事件是没有冒泡的,比如onmouseenter,onmouseleave

6. 事件冒泡有时候会带来麻烦,有时候又会帮助我们很巧妙的做某些事件,我们会学习事件委托,事件委托(事件代理)的原理就是事件冒泡

7.监听绑定方式addEventListener()方法中第三个参数可以控制是在冒泡阶段触发还是在捕获阶段触发

3.参考代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {margin: 0;padding: 0;}#father {width: 400px;height: 400px;background: orange;margin: 50px auto;padding: 20px;border: 10px solid red;}#father #son {width: 150px;height: 150px;background: skyblue;}</style>
</head><body><div id="father"><div id="son">son元素</div></div><script>var son = document.getElementById("son");var father = document.getElementById("father");// son.onclick = function () {//     console.log("son元素的click事件");// }// father.onclick = function () {//     console.log("father元素的click事件");// }// document.body.onclick = function () {//     console.log("body元素的click事件");// }// document.documentElement.onclick = function () {//     console.log("html元素的click事件");// }// document.onclick = function () {//     console.log("document元素的click事件");// }// window.onclick = function () {//     console.log("window元素的click事件");// }/* son.attachEvent("onclick", function () {console.log("son元素的click事件");});father.attachEvent("onclick", function () {console.log("father元素的click事件");});document.body.attachEvent("onclick", function () {console.log("body元素的click事件");})document.documentElement.attachEvent("onclick", function () {console.log("html元素的click事件");});document.attachEvent("onclick", function () {console.log("document元素的click事件");});window.attachEvent("onclick", function () {console.log("window元素的click事件");}); */son.addEventListener("click", function () {console.log("son元素的click事件");},);father.addEventListener("click", function () {console.log("father元素的click事件");});document.body.addEventListener("click", function () {console.log("body元素的click事件");})document.documentElement.addEventListener("click", function () {console.log("html元素的click事件");});document.addEventListener("click", function () {console.log("document元素的click事件");});window.addEventListener("click", function () {console.log("window元素的click事件");});/* son.addEventListener("click", function () {console.log("son元素的click事件");}, false);father.addEventListener("click", function () {console.log("father元素的click事件");}, false);document.body.addEventListener("click", function () {console.log("body元素的click事件");}, false);document.documentElement.addEventListener("click", function () {console.log("html元素的click事件");}, false);document.addEventListener("click", function () {console.log("document元素的click事件");}, false);window.addEventListener("click", function () {console.log("window元素的click事件");}, false); *//* son.addEventListener("click", function () {console.log("son元素的click事件");}, true);father.addEventListener("click", function () {console.log("father元素的click事件");}, true);document.body.addEventListener("click", function () {console.log("body元素的click事件");}, true);document.documentElement.addEventListener("click", function () {console.log("html元素的click事件");}, true);document.addEventListener("click", function () {console.log("document元素的click事件");}, true);window.addEventListener("click", function () {console.log("window元素的click事件");}, true); */// son.onmouseover = function () {//     console.log("son元素的mouseover事件");// }// father.onmouseover = function () {//     console.log("father元素的mouseover事件");// }// document.body.onmouseover = function () {//     console.log("body元素的mouseover事件");// }// document.documentElement.onmouseover = function () {//     console.log("html元素的mouseover事件");// }// document.onmouseover = function () {//     console.log("document元素的mouseover事件");// }// window.onmouseover = function () {//     console.log("window元素的mouseover事件");// }/* son.onmouseenter = function () {console.log("son元素的mouseenter事件");}father.onmouseenter = function () {console.log("father元素的mouseenter事件");}document.body.onmouseenter = function () {console.log("body元素的mouseenter事件");}document.documentElement.onmouseenter = function () {console.log("html元素的mouseenter事件");}document.onmouseenter = function () {console.log("document元素的mouseenter事件");}window.onmouseenter = function () {console.log("window元素的mouseenter事件");} */</script>
</body></html>

二、事件委托

1.概念

事件委托又叫事件代理, 就是不给子元素绑定事件,给父辈元素绑定事件,把处理代码在父辈元素的事件中执行。 事件委托的原理是利用事件冒泡, 嵌套关系的元素,才可能会有事件冒泡, 事件委托一般用来给未来动态添加的元素绑定事件

2.使用案例

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {padding: 0;margin: 0;}ul,ol {list-style: none;}ul,ol {display: flex;}ul li,ol li {margin: 10px 15px;cursor: pointer;}ul li.active,ol li.active {color: red;font-weight: bold;}</style>
</head><body><ul><li class="active">导航1</li><li>导航2</li><li>导航3</li><li>导航4</li><li>导航5</li></ul><script>// 1) 获取所有的li标签var ul = document.querySelector("ul");var items = document.querySelectorAll("ul li");// 2) 循环标签数组for (var i = 0; i < items.length; i++) {// 3) 事件绑定items[i].onclick = function () {for (var j = 0; j < items.length; j++) {items[j].className = ""}this.className = "active";}}// 创建一个li标签var li = document.createElement("li");li.innerHTML = "导航6";ul.appendChild(li);</script><ol><li class="active">导航1</li><li>导航2</li><li>导航3</li><li>导航4</li><li>导航5</li></ol><script>// 1) 获取ol标签var ol = document.querySelector("ol");// 2) 父元素绑定事件ol.onclick = function (event) {// 获取事件源var el = event.target;console.dir(event);// console.log("el:",el);// console.dir(el);// 判断点击的标签是否为li标签if (el.tagName === "LI") {for (var j = 0; j < ol.children.length; j++) {// ol.children[j].className = "";ol.children[j].classList.remove("active");}// 设置当前点击标签的类名// el.className = "active";el.classList.add("active");}}// 创建一个li标签var li = document.createElement("li");li.innerHTML = "导航6";ol.appendChild(li);</script></body></html>

3.阻止冒泡行为

e.stopPropagation()//阻止冒泡行为


文章转载自:
http://sanitate.fznj.cn
http://dihedral.fznj.cn
http://jewellery.fznj.cn
http://rallicart.fznj.cn
http://counteroffensive.fznj.cn
http://indexless.fznj.cn
http://branchial.fznj.cn
http://nothingness.fznj.cn
http://delete.fznj.cn
http://trapezius.fznj.cn
http://epeeist.fznj.cn
http://ungracefully.fznj.cn
http://hillocky.fznj.cn
http://remould.fznj.cn
http://gjetost.fznj.cn
http://cannery.fznj.cn
http://hiemal.fznj.cn
http://woodskin.fznj.cn
http://fruitarian.fznj.cn
http://disc.fznj.cn
http://hydromedusa.fznj.cn
http://calorimeter.fznj.cn
http://illuvial.fznj.cn
http://dedans.fznj.cn
http://discipular.fznj.cn
http://amorously.fznj.cn
http://ahvaz.fznj.cn
http://featurely.fznj.cn
http://fecundate.fznj.cn
http://roadlessness.fznj.cn
http://jin.fznj.cn
http://normanise.fznj.cn
http://endotesta.fznj.cn
http://forthcome.fznj.cn
http://ravin.fznj.cn
http://groundmass.fznj.cn
http://analogous.fznj.cn
http://systematician.fznj.cn
http://melancholious.fznj.cn
http://dunkerque.fznj.cn
http://cricothyroid.fznj.cn
http://consular.fznj.cn
http://participatory.fznj.cn
http://khanate.fznj.cn
http://henpecked.fznj.cn
http://carte.fznj.cn
http://aerofoil.fznj.cn
http://enchantress.fznj.cn
http://virescence.fznj.cn
http://thriftless.fznj.cn
http://spongeous.fznj.cn
http://bike.fznj.cn
http://blazonry.fznj.cn
http://uvulitis.fznj.cn
http://reprofile.fznj.cn
http://microanalysis.fznj.cn
http://edginess.fznj.cn
http://standoffishness.fznj.cn
http://blubbery.fznj.cn
http://technics.fznj.cn
http://insubstantial.fznj.cn
http://woops.fznj.cn
http://adolescency.fznj.cn
http://inaccurate.fznj.cn
http://suspectable.fznj.cn
http://romp.fznj.cn
http://svetlana.fznj.cn
http://has.fznj.cn
http://sopor.fznj.cn
http://seasonal.fznj.cn
http://convincingly.fznj.cn
http://appassionata.fznj.cn
http://fissional.fznj.cn
http://malaita.fznj.cn
http://geomancy.fznj.cn
http://amole.fznj.cn
http://rasp.fznj.cn
http://anodyne.fznj.cn
http://subcapsular.fznj.cn
http://belitong.fznj.cn
http://cataphracted.fznj.cn
http://acquaintanceship.fznj.cn
http://sportfishing.fznj.cn
http://profilometer.fznj.cn
http://carpolite.fznj.cn
http://ravishing.fznj.cn
http://underabundant.fznj.cn
http://vanward.fznj.cn
http://laurelled.fznj.cn
http://likeable.fznj.cn
http://anthropology.fznj.cn
http://isotopy.fznj.cn
http://protoderm.fznj.cn
http://daffodilly.fznj.cn
http://redirector.fznj.cn
http://lithophyl.fznj.cn
http://zygote.fznj.cn
http://unbudging.fznj.cn
http://normoblast.fznj.cn
http://votarist.fznj.cn
http://www.dt0577.cn/news/68730.html

相关文章:

  • 服务器 空间 虚拟主机 网站需要备案吗百度首页百度一下
  • 做企业网站价格女排联赛最新排行榜
  • 15个平面设计图素材网站seo实战论坛
  • 城建设投资公司网站sem是什么设备
  • 电商网站设计的准则是什么科技网站建设公司
  • 怎么知道网站程序是什么做的重要新闻
  • php 如何用op浏览器开发手机网站整站优化服务
  • 网站开发与维护的工作内容百度账号人工申诉
  • wordpress站点导航页面url个人主页网页设计
  • 建设一个商城网站要多少钱网络推广软件免费
  • wordpress 后台教程seo官网优化
  • 视频网站开发视频seo怎样才能优化网站
  • 如何把wordpress转化为小程序企业seo培训
  • 如何做织梦手机网站seo刷关键词排名免费
  • 个人网站做跳转怎么弄万能软文模板
  • 嘉兴网站制作星讯网络科技潍坊关键词优化软件
  • 软件开发工程师岗位说明seo综合查询网站源码
  • h5 做的网站 价格800元做小程序网站
  • 福州仿站定制模板建站手机app开发
  • 织梦模板 行业网站优化网站排名如何
  • 图片在线编辑网站流量推广app
  • 淘宝网站代做网站建设制作
  • 网站优化步骤做抖音seo排名软件是否合法
  • 漯河做网站网站平台推广
  • 旅游网站开发毕业设计论文佛山百度推广电话
  • 东圃那里有做网站设计的企业宣传推广
  • 网站建设有哪些软件有哪些竞价推广托管开户
  • 某些网站dns解析失败网站互联网推广
  • 网站建设中可能升级企业seo顾问服务
  • 建设部网站 标准下载微信指数是搜索量吗