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

JavaScript -- 总结 9 (小白)

事件对象event

<!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>.father {width: 400px;height: 400px;border: 1px solid #000;}.son {width: 200px;height: 200px;background-color: #f00;margin: 100px auto;}</style>
</head><body><button>点击</button><div class="father"><div class="son"></div></div><a href="">点击</a><script>// 事件三要素: 1.事件源(给谁绑定事件) 2.事件类型(绑定一个什么事件)  3.事件处理程序(绑定事件后要干嘛)var btn = document.getElementsByTagName("button")[0];// 单击事件// 事件对象 event btn.onclick = function (e) {console.log("点击了");console.log(e);// 属性// 事件的触发者console.log(e.target);}var father = document.getElementsByClassName("father")[0];father.onclick = function (e) {console.log("点击");// 事件触发者console.log(e.target);console.log(e.type);// console.log(e.currentTarget);}// 获取a标签var link = document.getElementsByTagName("a")[0];link.onclick = function (e) {console.log("点击标签");// 阻止默认事件e.preventDefault();}// 键盘事件document.body.onkeydown = function (e) {// 键码console.log(e.keyCode);}</script>
</body></html>

鼠标事件

<!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>.box {width: 400px;height: 400px;border: 1px solid #000;overflow: auto;}.son {width: 200px;height: 800px;background-color: pink;margin: 100px auto;}</style>
</head><body><div class="box"><div class="son"></div></div><script>var box = document.getElementsByClassName("box")[0];// 进入子元素会再次触发box.onmouseover = function () {console.log("鼠标刚进入");}box.onmouseenter = function () {console.log("鼠标完全进入");}box.onmouseout = function () {console.log("鼠标刚要离开");}box.onmouseleave = function () {console.log("鼠标完全离开");}box.onmousemove = function () {console.log("鼠标移动");}box.onmousedown = function () {console.log("鼠标按下");}box.onmouseup = function () {console.log("鼠标弹起");}box.ondblclick = function () {console.log("鼠标双击");}box.onmousewheel = function () {console.log("鼠标滚轮滚动");}box.onscroll = function () {console.log("滚动条滚动");}box.oncontextmenu = function () {console.log("右击菜单");}</script>
</body></html>

键盘事件

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><script>document.body.onkeydown = function (e) {console.log("键盘按下");console.log(e);}document.body.onkeypress = function () {console.log("按着不放");}document.body.onkeyup = function () {console.log("键盘弹起");}</script>
</body></html>

窗口事件

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script>window.onload = function () {var box = document.getElementsByClassName("box")[0];console.log(box);}</script>
</head><body><div class="box"></div><script>// onload window.onload = function () {console.log("资源加载完成");}window.onresize = function () {console.log("窗口大小改变");}window.onunload = function () {console.log("关闭窗口");}</script>
</body></html>

 表单事件

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body><input type="text"><!-- action 提交后的跳转页面 --><form action="./01-DOM事件.html"><input type="text" name="" id=""><input type="submit"><input type="reset"></form><script>var inp = document.getElementsByTagName("input")[0];var form = document.getElementsByTagName("form")[0];// 内容改变事件inp.onchange = function () {console.log("内容改变");}// 获取焦点inp.onfocus = function () {console.log("获取焦点");}// 失去焦点inp.onblur = function () {console.log("失去焦点");}// 输入事件inp.oninput = function () {console.log("输入事件");}// 提交事件form.onsubmit = function () {console.log("提交成功");}// 重置事件form.onreset = function(){console.log("重置");}</script>
</body></html>

注册事件处理程序

<!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>.box {width: 200px;height: 200px;background-color: #f00;}</style>
</head><body><!-- <button onclick="alert('点击')">点击</button> --><div class="box"></div><script>var box = document.getElementsByClassName("box")[0];// 1. on 事件box.onclick = function () {console.log("on 事件");}box.onclick = function () {console.log("第二个事件");}// 2.addEventListener()box.addEventListener("click", function () {console.log("事件1");});box.addEventListener("click", function () {console.log("事件2");});</script>
</body></html>

事件冒泡

<!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>.father {width: 400px;height: 400px;background-color: #f00;}.son1 {width: 200px;height: 200px;background-color: #0f0;}.son2 {width: 100px;height: 100px;background-color: #00f;}</style>
</head><body><div class="father"><div class="son1"><div class="son2"></div></div></div><script>var father = document.getElementsByClassName("father")[0];var son1 = document.getElementsByClassName("son1")[0];var son2 = document.getElementsByClassName("son2")[0];father.onmouseover = function (e) {console.log("进入" + e.target.className);}father.onclick = function () {console.log(this);}son1.onclick = function (e) {console.log(this);// 阻止事件冒泡e.stopPropagation();}son2.onclick = function (e) {console.log(this);// e.stopPropagation();}// 事件传递过程// 事件捕获// 目标阶段 (事件执行)// `事件冒泡`: 当为多个嵌套的元素设置了相同的事件处理程序,它们将触发事件冒泡机制。//            在事件冒泡中,最内部的元素将首先触发其事件,然后是栈内的下一个元素触发该事件,以此类推,//            直到到达最外面的元素。如果把事件处理程序指定给所有的元素,那么这些事件将依次触发。// 阻止事件冒泡</script></body></html>

事件委托

<!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>.nav {margin: 0;padding: 0;list-style: none;width: 700px;height: 30px;margin: 30px auto;}li {width: 100px;height: 30px;text-align: center;line-height: 30px;border: 1px solid #ccc;box-sizing: border-box;float: left;}</style>
</head><body><ul class="nav"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><script>// 事件委托:将原本绑定在子元素身上的事件,现在绑定到父元素身上,利用事件冒泡机制和 事件对象,触发当前事件。var nav = document.getElementsByClassName("nav")[0];nav.onclick = function (e) {// 事件的触发者// console.log(e.target);e.target.style.backgroundColor = "pink";}</script>
</body></html>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • k8s使用kustomize来部署应用
  • 排序算法1:堆排序,直接插入排序与希尔排序
  • System Verilog--$scanf应用举例
  • 学习日志8.7--防火墙安全策略
  • Hadoop单机及集群部署
  • html--前端
  • 前端构建工具|vite快速入门
  • DVWA(SQL注入)medium、high
  • ESXI加入VMware现有集群提示常规性错误
  • RabbitMQ之基于注解声明队列交换机:使用@RabbitListener实现消息监听
  • Java日志框架
  • npm、pnpm、yarn镜像源设置
  • 深入探究Liunx服务器内存:模拟程序实际占用与缓存占用内存
  • 深入理解 Go 语言信号量 Semaphore
  • 数据库事务( 五 ) Spring管理事务的几道面试题
  • 【React系列】如何构建React应用程序
  • conda常用的命令
  • java中具有继承关系的类及其对象初始化顺序
  • Laravel 中的一个后期静态绑定
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • PermissionScope Swift4 兼容问题
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • Vue全家桶实现一个Web App
  • Xmanager 远程桌面 CentOS 7
  • 从重复到重用
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 力扣(LeetCode)22
  • 名企6年Java程序员的工作总结,写给在迷茫中的你!
  • 新书推荐|Windows黑客编程技术详解
  • 在Unity中实现一个简单的消息管理器
  • 函数计算新功能-----支持C#函数
  • ​如何使用QGIS制作三维建筑
  • ![CDATA[ ]] 是什么东东
  • # centos7下FFmpeg环境部署记录
  • $.proxy和$.extend
  • (1)Android开发优化---------UI优化
  • (11)工业界推荐系统-小红书推荐场景及内部实践【粗排三塔模型】
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (Windows环境)FFMPEG编译,包含编译x264以及x265
  • (附源码)php新闻发布平台 毕业设计 141646
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • **PHP二维数组遍历时同时赋值
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划
  • .NET HttpWebRequest、WebClient、HttpClient
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .Net Web项目创建比较不错的参考文章
  • .Net 应用中使用dot trace进行性能诊断
  • .net通用权限框架B/S (三)--MODEL层(2)
  • .net知识和学习方法系列(二十一)CLR-枚举