回车事件

监听页面回车事件:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>test.html</title>

		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">

		<script type="text/javascript">
			document.onkeydown = function(e) {
				var ie = navigator.appName == "Microsoft Internet Explorer";
				if (ie) {
					if (event.keyCode == 13) {
						alert("你敲回车!");
					}
				} else {
					if (e.which == 13) {//firefox
						alert("你敲回车!");
					}
				}
			}
		</script>

	</head>

	<body>
		敲敲回车看
	</body>
</html>

 

 

在输入框时监听回车事件:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>test.html</title>
	
    <script type="text/javascript">
	    function keyListener(e){
	    	var ie=navigator.appName=="Microsoft Internet Explorer";
	    	if(ie){
	    		if(event.keyCode==13) {
	    			alert("你敲回车!");
	    		}
	    	}else{
	    		if(e.which==13) {//firefox
	    			alert("你敲回车!");
	    		}
	    	}
	    }
    </script>

  </head>
  
  <body>
  	<!-- onpaste是粘贴事件 -->
    <input id="tip" type="text" value=""  onkeydown="keyListener(event)" onpaste="return false;" />
  </body>
</html>
 

 

 

 

你可能感兴趣的:(JavaScript,html,IE,Microsoft,firefox)