DOM事件绑定
DOM 0级别事件绑定
DOM 0级别:DOM最早版本的事件绑定方式,被所有浏览器兼容
- HTML绑定
<body>
<input type="button" id="btn" value="click me" onclick="show()" />
<script type="text/javascript">
function show(){
console.log("aaa");
}
</script>
</body>
- JS绑定
JS绑定事件时,函数名后不能加(),否则会在页面加载时立即执行。
<body>
<input type="button" id="btn" value="click me" />
<script type="text/javascript">
document.getElementById("btn").onclick=show;
function show(){
console.log("aaa");
}
</script>
</body>
JS绑定事件时也可绑定匿名函数
<body>
<input type="button" id="btn" value="click me" />
<script type="text/javascript">
document.getElementById("btn").onclick=function(){
console.log("aaa");
}
</script>
</body>
DOM 2级别事件绑定
DOM 2级别:添加了两个监听方法来添加和移除事件处理程序
- addEventListener()----------添加事件
<body>
<input type="button" id="btn" value="click me" />
<script type="text/javascript">
document.getElementById("btn").addEventListener('click',show);
function show(){
console.log("aaa");
}
</script>
</body>
'click’为事件名,不加on。show为函数名,不加引号和括号().
可以绑定匿名函数
<body>
<input type="button" id="btn" value="click me" />
<script type="text/javascript">
document.getElementById("btn").addEventListener('click',function(){
console.log("aaa");
});
</script>
</body>
- removeEventListener()----------移除事件
无法移除匿名添加的函数
<body>
<input type="button" id="btn" value="添加" />
<script type="text/javascript">
document.getElementById("btn").addEventListener("click",show);
document.getElementById("btn").removeEventListener('click',show);
function show(){
console.log("aaa");
}
</script>
</body>
无法移除DOM 0级别事件绑定
<body>
<input type="button" id="btn" value="添加" onclick="show()"/>
<script type="text/javascript">
//document.getElementById("btn").οnclick=show;
document.getElementById("btn").removeEventListener('click',show);
function show(){
console.log("aaa");
}
</script>
</body>
DOM 0级别事件和DOM 2级别事件区别
DOM 0级别事件绑定多个函数,前面绑定的函数会被后面的覆盖
DOM 2级别事件可以绑定多个函数,按顺序执行
<body>
<input type="button" id="btn" value="添加" />
<script type="text/javascript">
document.getElementById("btn").onclick=function(){
console.log(1);
}
document.getElementById("btn").onclick=function(){
console.log(2);
}
document.getElementById("btn").onclick=function(){
console.log(3);
}
</script>
</body>
<body>
<input type="button" id="btn" value="添加" />
<script type="text/javascript">
document.getElementById("btn").addEventListener('click',function(){
console.log(1);
})
document.getElementById("btn").addEventListener('click',function(){
console.log(2);
})
document.getElementById("btn").addEventListener('click',function(){
console.log(3);
})
</script>
</body>
DOM 0级事件与DOM 2级事件互不影响
<body>
<input type="button" id="btn" value="添加" />
<script type="text/javascript">
document.getElementById("btn").addEventListener('click',function(){
console.log(1);
})
document.getElementById("btn").onclick=function(){
console.log(1);
}
</script>
</body>
事件传参
用JS绑定传参将无法成功,因为加了括号会直接执行,但可以利用匿名函数
<body>
<input type="button" id="btn" value="添加" />
<script type="text/javascript">
document.getElementById("btn").onclick=function(){
foo('hello');
}
function foo(x){
console.log(x);
}
</script>
</body>
可以使用行内绑定(HTML绑定),注意要加引号
<body>
<input type="button" id="btn" value="添加" onclick="foo(1)"/>
<script type="text/javascript">
function foo(x){
console.log(x);
}
</script>
</body>
event.target会返回触发该事件的目标元素
<body>
<input type="button" id="btn" value="1" onclick="foo()"/>
<script type="text/javascript">
function foo(){
console.log(event.target.value);
}
</script>
</body>
行内调用时函数传入this,this代表该元素本身
<body>
<input type="button" id="btn" value="1" onclick="foo(this)"/>
<script type="text/javascript">
function foo(x){
console.log(x.value);
}
</script>
</body>
常用事件
event.code可判断按下的按键
event.clientX 返回当事件被触发时鼠标指针向对于浏览器页面(或客户区)的水平坐标。
event.clientY 返回当事件被触发时鼠标指针向对于浏览器页面(或客户区)的垂直坐标。