JavaScript事件应用

一:  为按钮绑定单击事件

<html>
<head>

<title></title>
<script type="text/javascript">
function showValue(btn){
alert(btn.value);
}
//console从控制台输出

</script>

 </head>

<body>
<!--此处在调用showValue 函数时传入一个this关键字作为参数,this在此处表示当前按钮对象-->
<input type="button" value="按钮1" onclick="showValue(this);"><br>
<input type="button" value="按钮2" onclick="showValue(this);"><br>
<input type="button" value="按钮3" onclick="showValue(this);"><br>
</body>
</html>

 

 

二: 为文本框绑定光标事件

需要为文本框绑定onblue(失去光标事件)事件,同时函数只为这一个文本框服务,所以可采用匿名函数的方式进行处理

<html >
<head>

<title>无标题文档</title>
<script type="text/javascript">
//这里需保证页面文档加载完毕,否则无法取得文本框对象
window.onload=function(){
       //通过document.getElementById()函数获取文本框对象
       var cutTxt =document.getElementById('txtResult');
        //为文本框绑定onblur事件
         cutTxt.onblur=function(){
        //this表示调用此函数的当前文本框对象
         alert(this.value);
        }
  }

</script>

</head>

<body>
<input type="text" id="txtResult">
<input type="button" value="按钮"><br>

</body>
</html>

posted on 2018-05-09 21:28  life阳光城  阅读(160)  评论(0编辑  收藏  举报