prompt彈出消息對話框,通常用於詢問一些需要與用戶交互的信息。
彈出消息對話框(包含一個確定按鈕、取消按鈕與一個文本輸入框)。
語法:
prompt(str1, str2);
參數說明:
str1: 要顯示在消息對話框中的文本,不可修改
str2:文本框中的内容,可以修改
返回值:
1. 點擊確定按鈕,文本框中的内容將作爲函數返回值
2. 點擊取消按鈕,將返回null
看看下面代碼:
var myname=prompt("請輸入你的姓名:"); if(myname!=null){ alert("你好"+myname); }else{ alert("你好 my friend."); }
結果:
注:在用戶點擊對話框的按鈕前,不能進行任何其它操作。
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>prompt</title> <script type="text/javascript"> function rec(){ var score; score = prompt("請輸入您的成績!!!"); if(score>=90){ document.write("你很棒!"); }else if(score>=75){ document.write("不錯嘛!"); }else if(score>=60){ document.write("要加油!"); }else{ document.write("要努力啦!"); } } </script> </head> <body> <input name="button" type="button" value="點擊我,對成績" onClick="rec()" /> </body> </html>