<!DOCTYPE html>
<html>
<body>
<p>当您输入输入字段(FORM 的子)时,会触发一个将背景颜色设置为黄色的函数。当您离开输入字段时,会触发一个删除背景颜色的函数。</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>