js添加事件的通用方法推荐
js添加事件的通用方法推荐
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <p id="p1">测试添加事件:firefox使用addEventListener,ie使用attachEvent<br> 点击此p标签,绑定了2个弹出事件</p> <script> function test1() { alert("test1"); } function test2(){ alert("test2"); } //添加事件通用方法 function addEvent(element,e,fn) { //firefox使用addEventListener,来添加事件 if(element.addEventListener) { element.addEventListener(e,fn,false); } //ie使用attachEvent,来添加事件 else { element.attachEvent("on"+e,fn); } } window.onload = function(){ var element = document.getElementById("p1"); addEvent(element,"click",test1); addEvent(element,"click",test2); } </script> </body> </html>
相关推荐
IdeaElements 2020-08-19
阿斌Elements 2020-06-11
88274956 2020-11-03
Zhongmeishijue 2020-09-10
runner 2020-09-01
梦的天空 2020-08-25
luvhl 2020-08-17
移动开发与培训 2020-08-16
ReunionIsland 2020-08-16
lyqdanang 2020-08-16
NARUTOLUOLUO 2020-08-03
MyNameIsXiaoLai 2020-07-08
星辰的笔记 2020-07-04
csstpeixun 2020-06-28
letheashura 2020-06-26
liaoxuewu 2020-06-26
OldBowl 2020-06-26
北京老苏 2020-06-25
Luffyying 2020-06-25