首页 > 代码库 > 按键事件

按键事件

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus?">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>聚焦离焦事件</title>
 </head>
 <script type="text/javascript">
 <!--
function fun(obj,e){
    //拿到按键的asc码
    var key = e.keyCode;
    obj.value = key;
}
 //-->
 </script>

 <body>
 <input type="text" name="" onkeypress = "fun(this,event)"> <!--显示按键的ascii-->
 </body>
</html>

 提交事件

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus?">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>提交事件</title>
 </head>
 <script type="text/javascript">
 <!--
function check(form){
    //拿到文本框中的内容
    var txt = form.username.value;
    //判断内容
    if(txt == ""){
        document.getElementById("sname").innerHTML = "<font color = red>姓名必填</font>";
        form.username.focus();
        return false;
    }
    return true;
}


 //-->
 </script>

 <body>
    <form method = "post" action="2.html" onsubmit = "return check(this)">
        姓名:<input type="text" name="username"><span id="sname"></span><br>
             <input type="submit" value=http://www.mamicode.com/"提交">
    </form>
 </body>
</html>

 

按键事件