- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <input type="text" placeholder="输入密码" id="txt" />
- <button id="btn">检测</button>
- <script>
- // 4.判断用户输入的密码是否正确,如果是123,则为正确,如果不是,就错误。
- var oTxt = document.getElementById("txt"),
- oBtn = document.getElementById("btn"),
- num = null;
- oBtn.onclick = function (){
- num = oTxt.value;
- if(num == "123"){
- console.log("密码正确!");
- oTxt.value = "";
- }else{
- console.log("密码不正确");
- oTxt.value = "";
- }
- }
- </script>
- </body>
- </html>