我正在制作一个关于 HTML 的教程,并正在尝试制作这样的系统。将出现一个问题,您需要在文本框中输入正确答案。根据您的回答,计算机将显示其他程序中编码的不同 HTML 页面。问题是我无法使用 JS 错误标签来抛出页面,也无法更改整个页面,而 document.write 可以更改整个页面,但不能更改图像。
我尝试过
try{
if(x != "the correct answer") throw "the link of the wrong page"
if(x == "the correct answer") throw "the link of the right page"
}
它没有抛出链接或更改 HTML 页面,而是没有显示任何内容。
我什至知道如果在 HTML 页面加载后调用 JS
document.write()
,JS 会删除所有 HTML,所以我尝试了
<button type="button" onclick=document.write(<a href="the link of the wrong page"></a>)Submit</button>
但后来我意识到 document.write() 是无法控制的(调用时显示相同的内容)并且不能抛出链接或页面。
您正在使用 try catch 语句。您应该同时使用 catch。
访问:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
试试这个:
Try{
If(my condition…)throw new Error(‘my error’)
}catch(e){
If(e===‘my error’)window.open(…)
}