js判斷鼠標點擊的是哪個鍵

鼠標事件

 

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	
	<body>
		<div>
			<button onmousedown="whichButton(event)">點擊</button>
		</div>
	</body>
	<script type="text/javascript">
		
		function whichButton(event)
		{
			var btnNum = event.button;
			if (btnNum==2)
			{
				alert("您點擊了鼠標右鍵!")
			}
			else if(btnNum==0)
			{
				alert("您點擊了鼠標左鍵!")
			}
			else if(btnNum==1)
			{
				alert("您點擊了鼠標中鍵!");
			}
			else
			{
				alert("您點擊了" + btnNum+ "號鍵,我不能確定它的名稱。");
			}
		}
	
	</script>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章