<html>
<body>
<p>当在 input 框输入时(获取焦点,FORM表单是子元素),一个函数将被触发,并设置背景颜色为黄色。当离开 input 输入框时(失去焦点),另一个函数将被触发并移除背景颜色。</p>
<form id="myForm">
<input type="text" id="myInput">
</form>
<script>
var x = document.getElementById("myForm");
x.addEventListener("focus", myFocusFunction, true);
x.addEventListener("blur", myBlurFunction, true);
function myFocusFunction() {
document.getElementById("myInput").style.backgroundColor = "yellow";
}
function myBlurFunction() {
document.getElementById("myInput").style.backgroundColor = "";
}
</script>
</body>
</html>