JavaScript——輸入框、計時器
阿新 • • 發佈:2022-12-07
JavaScript——輸入框、計時器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body onload="get_time()"> <!-- <form action=""> --> <!-- register --> <p> username:<input type="text" id="userName"> <span style="color: red;"></span> </p> <p> password:<input type="password" id="passWord"> <span style="color: red;"></span> </p> <button id="suBtu">提交</button> <!-- </form> --> <!-- time --> <br> time:<span id='nowTime'></span> <br> <button id='start'>start</button> <button id="stop">stop</button> <script src="new_file.js"></script> </body> </html>
let suBtuEle = document.getElementById('suBtu') suBtuEle.onclick = function(){ let userNameEle = document.getElementById('userName') let passWordEle = document.getElementById('passWord') if (userNameEle.value === 'jason'){ userNameEle.nextElementSibling.innerText = 'username is not jason!' } if (passWordEle.value === '123'){ passWordEle.nextElementSibling.innerText = 'password is not 123!' } userNameEle.onfocus = function(){ userNameEle.nextElementSibling.innerText = '' } passWordEle.onfocus = function(){ passWordEle.nextElementSibling.innerText = '' } } // 獲取當前時間、開始、結束按鈕標籤 let timeEle = document.getElementById('nowTime') let startEle = document.getElementById('start') let stopEle = document.getElementById('stop') // 獲取當前時間 function get_time(){ var date=new Date(); var time=date.toLocaleString(); document.getElementById("nowTime").innerHTML=time; } t = setInterval("get_time();",1000); startEle.onclick = function(){ t = setInterval("get_time();",1000); } stopEle.onclick = function(){ clearInterval(t) }