我想为我哥哥的生日创建一个简单的浏览器游戏,所以我陷入了困境。这是我在[https://codepen.io/TehMerow/pen/RGQOEK]]处找到的代码。输入正确后,我希望JS键入下一级的可点击链接。我怎么做?另一种可能是,如果写下正确的输入,则使JS重定向到新页面,但对我来说似乎更复杂。请告诉我确切的位置在哪里插入缺少的行,因为否则我将不知道如何实现它。 TIA!
var area = document.querySelector('.box-text');
var input = document.querySelector('.cmd-text');
var box = document.querySelector('.box');
var title = `Welcome, to Shady Brook Hills`
var start = `
placeholder`;
var help = `
the commands are:
help: the current command.
stand up: stand up from the current position`
var credits = `
story writer: TehMerow;
coding: TehMerow;
`
function typeWrite(text, n, delay, area){
if(n < (text.length)){
area.innerText += text[n];
n++
}
setTimeout(function(){
typeWrite(text, n, delay, area);
}, delay)
}
typeWrite(title, 0, 0, area);
setTimeout(function(){
typeWrite(start, 0, 0, area);
}, 3000)
document.addEventListener('keydown', function(e){
e.preventDefault;
if(e.which === 13 ){
switch(input.value){
case 'help':
typeWrite('\n' + input.value + help, 0, 50, area);
break;
case '':
input.value = '';
break;
case 'credits':
typeWrite(credits, 0, 25, area);
break;
default:
typeWrite('\n' + "I don't know how to " + input.value, 0, 50, area);
break;
}
//ensure that bottom line is
//alays in view
box.scrollTop += 100;
//empty input
input.value = ''
}
}, false)
更新:我已经设法整理了一些可以像文字冒险游戏或简单的质量检查游戏那样工作的东西,但是仍然没有可点击链接的选项。
function myFunction() {
var text;
var answers = document.getElementById("myInput").value;
switch(answers) {
case "answer1":
text = "THAT IS THE CORRECT ANSWER. HERE IS YOUR LINK: https://www.link-which-should-be-clickable.com";
break;
case "answer2":
location="https://www.link-which-should-be-clickable.com";
break;
case "answer3":
txt.link("https://www.link-which-should-be-clickable.com")
break;
default:
text = "THAT IS NOT THE CORRECT ANSWER";
}
document.getElementById("demo").innerHTML = text;
}
“ answer1”只写文本,“ answer2”重定向到该页面,“ answer3”不执行任何操作。真的没有办法以这种形式使链接可点击吗?