代码编辑器:
x
1
<p>当在 input 框输入时(获取焦点,FORM表单是子元素),一个函数将被触发,并设置背景颜色为黄色。当离开 input 输入框时(失去焦点),另一个函数将被触发并移除背景颜色</p>
2
<form id="myForm">
3
<input type="text" id="myInput">
4
</form>
5
<p><strong>注意:</strong> Firefox 浏览器不支持 onfocusin 事件</p>
6
<script>
7
var x = document.getElementById("myForm");
8
x.addEventListener("focusin", myFocusFunction);
9
x.addEventListener("focusout", myBlurFunction);
10
function myFocusFunction() {
11
document.getElementById("myInput").style.backgroundColor = "yellow";
12
}
13
function myBlurFunction() {
14
document.getElementById("myInput").style.backgroundColor = "";
15
}
16
</script>
17