源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>编程字典</title> </head> <body> <p>当输入框获得焦点时,一个函数被触发,函数可以改变输入框的背景色</p> First name: <input type="text" id="fname" onfocus="myFunction(this.id)"><br> Last name: <input type="text" id="lname" onfocus="myFunction(this.id)"> <script> function myFunction(x) { document.getElementById(x).style.background = "yellow"; } </script> </body> </html>
运行结果