焦点事件会在页面获得或者失去焦点的时候触发。
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>