JavaScript-焦点事件

来源:互联网 发布:公司数据分析 编辑:程序博客网 时间:2024/05/08 19:35

焦点事件会在页面获得或者失去焦点的时候触发。
blur: 在元素失去焦点时触发。

<!DOCTYPE html><html lang="zh-cn"><head>    <meta charset="utf-8"/>    <title>lose focus event</title></head><body>    <script type="text/javascript">    function upperCase(){        var x = document.getElementById("fname").value;        document.getElementById("fname").value = x.toUpperCase();    }    </script>    输入你的姓名:    <input type="text" id="fname" onblur="upperCase()" /></body></html>

focus:在元素获得焦点时触发。

<!DOCTYPE html><html lang="zh-cn"><head>    <meta charset="utf-8"/>    <title>get focus event</title></head><body>    <script type="text/javascript">        function setStyle(x){            document.getElementById(x).style.background="yellow";        }    </script>First name:<input type="type" id="fname" onfocus="setStyle('fname')"/>Last name:<input type="type" id="lname" onfocus="setStyle(this.id)"></body></html>
原创粉丝点击