为input标签绑定事件的几种方式

为input标签绑定事件的几种方式

1.JavaScript原生态的方式,直接复制下面的代码就会有相应的效果

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>为标签绑定事件</title>
<script>
function danji(){
alert("hello javascript");
}
</script>
</head>
<body>
<p>单击按钮触发函数。</p>
<input type="button" value="添加"  id="submit" onclick="danji()">
</body>
</html>

 

2.jquery方式必须事先导入js的包才有效果!

<!DOCTYPE html>
<html>
   <head>
   <meta charset="utf-8">
   <title>为标签绑定事件</title>
   <script type="text/javascript" src="../../../framework/jquery/jquery-1.7.min.js"></script>  /*我导入的包是相对于我自己项目的相对路径,大家根据自己情况修改*/
   <script>
$("#submit").click(function(){
    alert("hello");
})
   </script>
   </head>
     <body>
<p>单击按钮触发函数。</p>
<input type="button" value="添加"  id="submit">
    </body>
<body>


第二种方式本质上可以为任何id绑定已存在的事件,比如鼠标双击事件,键盘按下事件,键盘释放事件,失去焦点事件等等,为某个id绑定事件用第二种方式基本没什么问题,均可以有相应的实现。

posted @ 2018-06-13 16:58  IT刑天  阅读(3519)  评论(0编辑  收藏  举报