当前位置:网站首页>DOM —— 事件绑定与解绑
DOM —— 事件绑定与解绑
2022-08-02 14:17:00 【z_小张同学】
概念:
事件,就是函数在某种状态下(是由浏览器设定的,也叫事件触发)被调用.JS捕捉到的发生在网页上的行为,然后执行提前设定好的程序,官方称为事件句柄,是交互体验的核心功能。
事件三要素
1.事件源
2.事件类型
3.事件处理程序(handler)
事件绑定的方式
(1)行内绑定方式:在标签行内 的事件值上写上标志"javaScript:后跟js代码"
<div class="box" onclick="console.log(666)">hello</div>(2)元素属性绑定方式:兼容性很好,但是一个元素的同一个时间上只能绑定一个处理程序(handler)
var box = document.querySelector(".box")
box.onclick = function() {
console.log("hello world");
}(3)给元素添加一个事件监听器:IE9以下不兼容,可以为一个事件绑定多个处理程序 (使用最多的方法)
var box = document.querySelector(".box")
box.addEventListener("click",function() {
console.log("hello");
})(4)给元素添加一个事件监听器(IE专有):一个事件同样可以绑定多个处理程序
var box = document.querySelector(".box")
box.attachEvent("click",function() {
console.log("hello");
})(5)多元素同事件同处理程序绑定方式(代理模式):父元素绑定事件 通过事件对象来区分用户触发的事件属于哪一个具体的对象
var box = document.querySelector(".box")
box.onclick=function(e) {
e.target
}事件解绑
(1)行内和属性解绑的事件 box.onclick = null
var box = document.querySelector(".box")
box.onclick = function() {
box.onclick = null
}(2)移除对应的元素的对应的监听程序
function fn() {
console.log(222);
}
var box = document.querySelector(".box")
box.addEventListener("click",fn)
// 解绑
box.removeEventListener("click",fn)边栏推荐
- The relationship between base classes and derived classes [inheritance] / polymorphism and virtual functions / [inheritance and polymorphism] abstract classes and simple factories
- 在命令行或者pycharm安装库时出现:ModuleNotFoundError: No module named ‘pip‘ 解决方法
- 【面经】被虐了之后,我翻烂了equals源码,总结如下
- 抽象类和接口 基本知识点复习
- 8.0以上MySQL的常见错误
- adb常用命令
- 【软件测试】基础篇
- Xshell 使用删除键乱码问题
- 关于导出聊天记录这件事……
- 【软件测试】selenium自动化测试2
猜你喜欢
随机推荐
RouteOS 导入至PVE
test2
Mysql开启日志并按天进行分割
OpenPose Basic Philosophy
关于机组的部分知识点随笔
【无标题】
nvm管理node版本 nodenpm不是内部或外部命令,也不是可运行的程序
【网络安全】学习笔记 --02 安全通信协议
Doubly linked list (normal iterators and const iterators)
PostgreSQL 协议数据样例
小知识点系列:数组与多维数组
【软件测试】性能测试理论
【Solidity智能合约基础】-- 基础运算与底层位运算
【软件测试】selenium自动化测试1
解决跨域的方法 --- Proxy
Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关...
APP版本更新通知流程测试要点
三大特殊类(String Object 包装类)与异常
Mysql-Explain与索引详解
Priority table and Ascll table









