為什么在prompt窗口點(diǎn)了取消還是會(huì)打開(kāi)新窗口的
function?openWindow(){ ????????var?clickConfirm?=?confirm("是否打開(kāi)網(wǎng)址輸入框?"); ????????if?(clickConfirm?==?true){ ????????????var?link?=?prompt("Please?input?the?website?address:","https://qq.com"); ????????????window.open(link,'_blank','width=500,height=600'); ????????} ????????else?{ ????????????alert("helloworld"); ????????} ????}
我想實(shí)現(xiàn)點(diǎn)擊確認(rèn)打開(kāi)新窗口,點(diǎn)擊取消彈alert,但是好像不行。
另外我想嘗試:
1、點(diǎn)擊按鈕先彈prompt窗口;點(diǎn)擊確認(rèn)再?gòu)棾鯿onfirm窗口;點(diǎn)擊取消彈alert
2、彈出confirm窗口,點(diǎn)擊確認(rèn)打開(kāi)新網(wǎng)頁(yè)窗口,點(diǎn)擊取消彈alert
試了很多個(gè)方式,但是都會(huì)提示else錯(cuò)誤等等,無(wú)法正常實(shí)現(xiàn)我需要的功能。比如如下代碼:
function?openWindow(){ ????????var?link?=?prompt("Please?input?the?website?address:","https://qq.com"); ????????if?(prompt?==?true){ ????????????var?clickConfirm?=?confirm("是否打開(kāi)網(wǎng)址輸入框?"); ????????????if?(clickConfirm?==?true){ ????????????????window.open(link,'_blank','width=500,height=600'); ????????????} ????????????else?{ ????????????????alert("helloworld"); ????????????} ????????} ????????else?{ ????????????alert("helloworld"); ????????} ????}
請(qǐng)大家指點(diǎn)一下,謝謝
2020-10-20
你第一個(gè)代碼段沒(méi)有問(wèn)題,可以按照預(yù)期運(yùn)行;:如果你把
if
?(clickConfirm ==
true
)
誤打成if
?(clickConfirm =
true
)
就會(huì)出現(xiàn)你描述的問(wèn)題,這段代碼和你測(cè)試時(shí)的代碼是否不同?你第二個(gè)代碼段錯(cuò)在對(duì)prompt方法返回值的理解。下面的代碼是對(duì)你的描述的一種實(shí)現(xiàn):2020-10-20
重寫(xiě)了第一段代碼:
根據(jù)樓上熱心朋友的解答,寫(xiě)了第二段:
2020-10-19
prompt的返回值是字符串或者null。所以第三行應(yīng)該是prompt(link!=null)