event 事件 attachEvent addEventListener绑定事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script>
			window.onload = function(){
				var oBtn = document.getElementById("btn1");
				
				//attachEvent(事件名,函数)
				//IE支持  火狐谷歌不支持
//				oBtn.attachEvent("onclick",function(){
//					alert("a");
//				});
//				
//				oBtn.attachEvent("onclick",function(){
//					alert("b");
//				});
				
				//ff 谷歌
				//addEventListener(事件名,函数,false/true)false冒泡 true捕捉
				//和attachEvent的不一样的是:事件不加on
//				oBtn.addEventListener("click",function(){
//					alert("a");
//				});
//				oBtn.addEventListener("click",function(){
//					alert("b");
//				});

				//兼容写法
				if(oBtn.attachEvent)
				{
					//ie下顺序相反 ie的一个bug
					oBtn.attachEvent("onclick",function(){
						alert("a");
					});
					
					oBtn.attachEvent("onclick",function(){
						alert("b");
					});
					
				}else{
					oBtn.addEventListener("click",function(){
						alert("a");
					});
					oBtn.addEventListener("click",function(){
						alert("b");
					});
				}
			}
		</script>
	</head>
	<body>
	<input type="button" value="点击" id="btn1" />
	</body>
</html>

  

转载于:https://www.cnblogs.com/mingjixiaohui/p/5269807.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值