對于輸入對話框的使用(prompt)有疑問
原先的代碼是醬紫的:
function openWindow(){
? ? ? ? ? var message=confirm("你是否要打開新窗口")
? ? ? ? ? ? ? if(message==true)
? ? ? ? ? ? ? ? { ?var str=prompt("請輸入你要打開的新窗口的網(wǎng)址","http://idcbgp.cn/");
? ? ? ? ? ? ? ? ? ? ? if(str==true)
? ? ? ? ? ? ? ? ? ? ? ?{window.open('http://idcbgp.cn/',width=400,height=500,menubar=no,toolbar=no);}
? ? ? ? ? ? ? ? ? ? ?else() ? {}
? ? ? ? ? ? ? ? }
? ? ? ? ? ? else() ? {}
}
然后問題出來了,在我在對話框中點擊確定時,沒有出現(xiàn)慕課網(wǎng)的網(wǎng)頁,而是重新返回到了我學習的網(wǎng)頁。我看了看下邊的評論,然后修改為if(str)就對了,但是我還是搞不懂這個prompt在進行判斷時是如何使用的?
2017-12-17
prompt 返回的是文本輸入框的信息,不是Boolean值,你把它和confirm弄混了,所以prompt的值不能作為if的判斷條件
2017-12-16
var str=prompt("請輸入你要打開的新窗口的網(wǎng)址","http://idcbgp.cn/");?//錯誤
后面的網(wǎng)址不要填寫,prompt(str1, str2);
str2 是你輸入的網(wǎng)址;
輸入網(wǎng)址點擊確定按鈕,文本框(str2)中的內(nèi)容將作為函數(shù)返回值
我的code
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title>??
? <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>? ?
? <script type ="text/javascript">??
? ? function openWindow(){
? ? // 新窗口打開時彈出確認框,是否打開
? ? ? ? var message = confirm("true and false");
? ? ? ? if(message){
? ? // 通過輸入對話框,確定打開的網(wǎng)址,默認為 http://idcbgp.cn/
? ? ? ? var score = prompt("請輸入網(wǎng)址:");
? ? //打開的窗口要求,寬400像素,高500像素,無菜單欄、無工具欄。
? ? ? ? ? ? window.open(score,"-blank","width=400,height=500");
? ? ? ? }
? ? }
? </script>?
?</head>?
?<body>?
? <input type="button" value="新窗口打開網(wǎng)站" onclick="openWindow()" />?
?</body>
</html>