菜单切换
FACESOHO知行者
心灵
记录
远方
赞赏工具
源代码:
点击运行
保存
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>小鸟启蒙(facesoho.com)</title> </head> <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>
运行结果