按键事件,提交事件,选择与改变事件

<!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="提交">
    </form>
 </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 fun(obj){
        alert(obj.value);
    }

    function fun2(v,index){
    
        alert(v + "--" + index);
    }
 //-->
 </script>
 <body>
  <input type="text" name="" onselect="fun(this)" on>
  <select onchange = "fun2(this.value, this.selectedIndex)">
        <option value="中国">中国</option>
        <option value="美国">美国</option>
        <option value="朝鲜">朝鲜</option>
  </select>
 </body>
</html>

原文地址:https://www.cnblogs.com/taiguyiba/p/6188571.html