abstract:課程中的案例代碼:增加了輸入為空時的彈窗和提交后輸入框獲取焦點<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>實戰(zhàn)熱身</ti
課程中的案例代碼:增加了輸入為空時的彈窗和提交后輸入框獲取焦點
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>實戰(zhàn)熱身</title> </head> <body> <input type="text" name="info"> <button>提交</button> <ul></ul> <script> //1、獲取元素 let input = document.getElementsByTagName('input')[0]; console.log(input); let btn = document.getElementsByTagName('button')[0]; console.log(btn); let ul = document.getElementsByTagName('ul')[0]; console.log(ul); btn.onclick = function () { let text = input.value; if (text === ''){ alert('請輸入內(nèi)容') input.focus(); }else { let li = document.createElement('li'); li.innerText = text; ul.appendChild(li); input.value = ''; input.focus(); } } </script> </body> </html>
運(yùn)行截圖:
總結(jié):
在案例的基礎(chǔ)上增加了輸入自動獲取焦點,同時把輸入為空時,進(jìn)行了判斷;
通過小案例熟悉了DOM操作。
Correcting teacher:天蓬老師Correction time:2019-07-01 17:42:02
Teacher's summary:其實這個就是業(yè)界經(jīng)典的ToDoList, 很多人都是通過這個JS入門的