<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript基础</title> </head> <body> <h2 id="header" style="color: blueviolet" onclick="alert(this.id)">草台班子学习频道</h2> <h2 id="header" style="color: blueviolet" onclick="alert(id)">草台班子学习频道</h2> <form action=""> <input name="ban" type="text" value="草台班子"> <button onclick="alert(ban.value)">显示内容</button> <br> <input name="username" type="text" placeholder="用户名"> <button onclick="check(username)">验证表单</button> <script> function check(username) { if(username.value.length===0){ alert('用户名空空如也') }else{ alert('验证通过,您输入的内容是:'+username.value) } } </script> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例