.onkeypress和.onkeydown的含义 以及补充知识document.onkeypress的含义

<!DOCTYPE HTML>
<html lang = 'en'>
	<head>
		<meta charset = 'UTF-8'/>
		<title>Document1</title>
		<style>
		</style>
	</head>
	<body>
		<script>
			//补充知识document.onkeyprss的含义:给页面绑定键盘事件
			document.onkeypress = function(){
    
    
				//.onkeypress按下字符键触发
				//字符键包括字母,数字,能在页面中显示的字符的键
				console.log('字符键');
			}
			document.onkeydown = function(){
    
    
				//.onkeydown按下任意键出发
				console.log('任意键');
			}
			/*
			//键盘事件对象中的altkey ctrlKey shiftKey对应三个键alt ctrl shift
			document.onkeydown = function(e){
				var evt = e || event;
				console.log(evt.altKey, evt.ctrlKey, evt.shiftKey);
				//判断是否按了某一个键
				//我们这里以回车键举例.keyCode
				if(evt.keyCode === 13){
					console.log('你按的是回车键');
				}else{
					console.log('你按得不是回车键');
				}
			}*/
		</script>
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_48727085/article/details/108235151