JS学习(13)----eventlistener

来源:互联网 发布:湖州美工培训学校 编辑:程序博客网 时间:2024/06/04 23:49

事件监听器的使用

eventListener的使用与前面事件的处理非常的相似,需要注意在事件监听器中事件的前缀“on”需要去掉,如onclick,在这里应该是click,onmouseover在这里是mouseover
用前面文章中的一个栗子做示范:

<!DOCTYPE html><html lang="en"><head>    <meta charset="utf-8"></head><body>    Text: <input type="text" id="txt"></body>    <script src="script.js"></script></body></html>
/* * @Author: Lin* @Date:   2017-07-24 10:17:36* @Last Modified by:   Lin* @Last Modified time: 2017-07-24 10:45:53*/document.getElementById("txt").addEventListener("focus", changeColor);function changeColor() {    document.getElementById("txt").style.backgroundColor = "lightBlue";}
document.getElementById("txt").addEventListener("focus", changeColor);// 或者是:document.getElementById("txt").addEventListener("focus", changeColor,true);

另外有removeEventListener.其使用方法与addEventListener相同。

原创粉丝点击