javascript 事件对象(一)

	document.onclick = function(){
	
		alert(this);             //HTMLDocument,this代表的是document
	};

	
	
	
	window.onload = function(){
	//document.onclick = box;      //因为box()函数被onclick绑定了,所以里面的this代表document
	};

	function box(){

		alert(this);          //HTMLDocument,代表是document
	};

	box();                    //object Window ,如果是在全局范围调用box(),那么this代表window
	
	function box(){
		alert(arguments.length);  //获取传参的输了
	};

	box();
	
	
	window.onload = function(){
	document.onkeydown = function(){
		//如果是事件处理函数绑定的函数,浏览器会默认传递一个参数,
		//这个参数就是event对象
		//alert(arguments[0]);    //MouseEvent,鼠标事件对象
		alert(arguments[0]);      //KeyboardEvent,键盘事件对象
	};

}	;



window.onload = function(){
	document.onmouseup = function(evt){
		
		if(getButton(evt) == 0){
			alert("左键");
		}
		if(getButton(evt) == 1){
			alert("中键");
		}
		if(getButton(evt) == 2){
			alert("右键");
		}
	
	};
};

//ps:window.event 这个属性IE是支持的,Chorme也是支持的
//ps:Chrome也是支持w3c的
//ps:如果说w3c和IE的都支持的话,那么以w3c为准

//跨浏览器鼠标按钮
function getButton(evt){
	var e = evt || window.event;
	if(evt){              //这个写前面
		return e.button;
	}
	else if(window.event){
		switch(e.button){
			//兼容IE和W3C
			case 1:{
				return 0;
			}
			case 4:{
				return 1;
			}
			case 2:{
				return 2;
			}
			//兼容360右击返回0的问题
			case 0:{
				return 2;
			}
		}
	}
	
	window.onload = function(){
	document.onclick = function(evt){
		var  e = evt || window.event;  //可视区坐标
		alert(e.clientX + document.documentElement.scrollTop + "," + e.clientY);
		//Chrome要用document.body.scrollTop
	};
};
	window.onload = function(){
	document.onclick = function(evt){
		var e = evt || window.event;
		
		alert(e.screenX + "," + e.screenY);  //屏幕坐标
	}

}

}

window.onload = function(){
	document.onclick = function(evt){
		alert(getKey(evt));
	}
}

function getKey(evt){
	var e = evt || window.event;
	var keys = [];
	
	if(e.shiftKey){
		keys.push("shift");
	}
	
	if(e.ctrlKey){
		keys.push("ctrl");
	}
	
	if(e.altKey){
		keys.push("alt");  //单击 + alt 和360的快捷键有冲突
	}
	
	return keys;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值