//为keyListener方法注册按键事件
document.onkeydown=keyListener;
function keyListener(e){
// 当按下回车键,执行我们的代码
if(e.keyCode == 13){
login();
}
}
另附登录界面详细代码
<!DOCTYPE HTML>
<html>
<head>
<title>考试后台登录</title>
<!-- Custom Theme files -->
<link href="../CSS/style1.css" rel="stylesheet" type="text/css" media="all"/>
<!-- Custom Theme files -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="后台登录" />
<!--Google Fonts-->
<!--<link href='//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
--><!--Google Fonts-->
</head>
<body>
<!--header start here-->
<div class="login-form">
<script type="text/javascript">
function login(){
var username=window.document.getElementById("username").value;
var password=window.document.getElementById("password").value;
if(password=="123456")
{
window.sessionStorage.setItem("username", username);
window.location.href="../index1.html";
}else{
alert("密码错误请输入正确的密码,例如:123456!");
return false;
}
}
//为button设置回车事件(百度搜索所得)
//为keyListener方法注册按键事件
document.onkeydown=keyListener;
function keyListener(e){
// 当按下回车键,执行我们的代码
if(e.keyCode == 13){
login();
}
}
</script>
<div class="top-login">
<span><img src="../Images/group.png" alt=""/></span>
</div>
<h1>登录</h1>
<div class="login-top">
<form>
<div class="login-ic">
<i ></i>
<input type="text" id="username" class="11" placeholder="请输入真实姓名"/>
<div class="clear"> </div>
</div>
<div class="login-ic">
<i class="icon"></i>
<input type="password" id="password" placeholder="请输入密码(默认密码123456)"/>
<div class="clear"> </div>
</div>
<div class="log-bwn">
<input type="button" value="登录考试" onclick="login()">
</div>
</form>
</div>
<p class="copy">© 2017 统本软件班考试系统</p>
</div>
<!--header start here-->
</body>
</html>