编辑下面的代码:【加编码】
<html> <body> <p>当在 input 框输入时(获取焦点,FORM表单是子元素),一个函数将被触发,并设置背景颜色为黄色。当离开 input 输入框时(失去焦点),另一个函数将被触发并移除背景颜色。</p> <form id="myForm"> <input type="text" id="myInput"> </form> <p><strong>注意:</strong> Firefox 浏览器不支持 onfocusin 事件。</p> <script> var x = document.getElementById("myForm"); x.addEventListener("focusin", myFocusFunction); x.addEventListener("focusout", myBlurFunction); function myFocusFunction() { document.getElementById("myInput").style.backgroundColor = "yellow"; } function myBlurFunction() { document.getElementById("myInput").style.backgroundColor = ""; } </script> </body> </html>
结果: 【此窗口】 帮助?
Try it Yourself - © 自强学堂