JavaScript 提示框示例
在你想要彈出文字框以便獲取使用者輸入時,提示對話方塊非常有用。因此,它讓你可以與使用者互動。使用者需要填寫該欄位,然後單擊確定。該對話方塊使用名為 prompt() 的方法顯示
以下是用於在 JavaScript 中顯示提示的程式碼 −
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>JavaScript Prompt() method</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to get the prompt dialogue box</h3> <script> let resultEle = document.querySelector(".result"); document.querySelector(".Btn").addEventListener("click", () => { let res = prompt("Are you sure"); if (res === null) { resultEle.innerHTML = "You pressed cancel"; } else { resultEle.innerHTML = "You entered " + res + " in the prompt box"; } }); </script> </body> </html>
輸出
在單擊“單擊此處”按鈕並在提示框中鍵入某個內容後 −
在提示框中單擊“確定”後 −
廣告