對(duì)于輸入對(duì)話框的使用(prompt)有疑問
原先的代碼是醬紫的:
function openWindow(){
? ? ? ? ? var message=confirm("你是否要打開新窗口")
? ? ? ? ? ? ? if(message==true)
? ? ? ? ? ? ? ? { ?var str=prompt("請(qǐng)輸入你要打開的新窗口的網(wǎng)址","http://idcbgp.cn/");
? ? ? ? ? ? ? ? ? ? ? if(str==true)
? ? ? ? ? ? ? ? ? ? ? ?{window.open('http://idcbgp.cn/',width=400,height=500,menubar=no,toolbar=no);}
? ? ? ? ? ? ? ? ? ? ?else() ? {}
? ? ? ? ? ? ? ? }
? ? ? ? ? ? else() ? {}
}
然后問題出來(lái)了,在我在對(duì)話框中點(diǎn)擊確定時(shí),沒有出現(xiàn)慕課網(wǎng)的網(wǎng)頁(yè),而是重新返回到了我學(xué)習(xí)的網(wǎng)頁(yè)。我看了看下邊的評(píng)論,然后修改為if(str)就對(duì)了,但是我還是搞不懂這個(gè)prompt在進(jìn)行判斷時(shí)是如何使用的?
2017-12-17
prompt 返回的是文本輸入框的信息,不是Boolean值,你把它和confirm弄混了,所以prompt的值不能作為if的判斷條件
2017-12-16
var str=prompt("請(qǐng)輸入你要打開的新窗口的網(wǎng)址","http://idcbgp.cn/");?//錯(cuò)誤
后面的網(wǎng)址不要填寫,prompt(str1, str2);
str2 是你輸入的網(wǎng)址;
輸入網(wǎng)址點(diǎn)擊確定按鈕,文本框(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(){
? ? // 新窗口打開時(shí)彈出確認(rèn)框,是否打開
? ? ? ? var message = confirm("true and false");
? ? ? ? if(message){
? ? // 通過(guò)輸入對(duì)話框,確定打開的網(wǎng)址,默認(rèn)為 http://idcbgp.cn/
? ? ? ? var score = prompt("請(qǐng)輸入網(wǎng)址:");
? ? //打開的窗口要求,寬400像素,高500像素,無(wú)菜單欄、無(wú)工具欄。
? ? ? ? ? ? window.open(score,"-blank","width=400,height=500");
? ? ? ? }
? ? }
? </script>?
?</head>?
?<body>?
? <input type="button" value="新窗口打開網(wǎng)站" onclick="openWindow()" />?
?</body>
</html>