onfocus事件:
html元素获取到焦点所触发的事件,通常用于 <input>输入框标签,<select>下拉列表标签,以及<a>超链接标签,不是所有html元素都支持onfocus事件。
onblur事件:
html元素失去焦点所触发的事件,与onfocus事件相对,通常用于<input>输入框标签,不是所有html元素都支持onblur事件。
代码展示:
<html>
<head>
<script type="text/javascript">
function focus_input() {
document.getElementById("first").style.backgroundColor = "purple";
}
function blur_input() {
document.getElementById("first").style.backgroundColor = "gold";
}
function focus_select() {
document.getElementById("second").style.backgroundColor = "purple";
}
function blur_select() {
document.getElementById("second").style.backgroundColor = "gold";
}
function focus_a() {
document.getElementById("third").style.backgroundColor = "purple";
}
function blur_a() {
document.getElementById("third").style.backgroundColor = "gold";
}
</script>
</head>
<body>
<input id="first" type="text" onfocus="focus_input()" onblur="blur_input()" />
<select id="second" onfocus="focus_select()" onblur="blur_select()">
<option value="option_1">option_1</option>
<option value="option_2">option_2</option>
<option value="option_3">option_3</option>
</select>
<a id="third" href="#" onfocus="focus_a()" onblur="blur_a()">a标签</a>
</body>
</html>
效果展示: