Javascript 未捕获的类型错误:无法设置 null 的属性“src”...选择您自己的冒险

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26046443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:14:21  来源:igfitidea点击:

Uncaught TypeError: Cannot set property 'src' of null...Choose your own adventure

javascriptnullsrc

提问by UltimateLink

I'm just learning javascript and I'm having some trouble. I'm making a choose your own adventure game where you go through a maze. There are pictures showing you what is going on. The pictures will change after each decision you make. I have a problem changing the pictures after typing in left or right. I keep getting Uncaught TypeError: Cannot set property 'src' of null error messages every time it tries to load the picture. Java says that the problem is occuring at Line 66

我只是在学习 javascript,但遇到了一些麻烦。我正在制作一个选择你自己的冒险游戏,你可以在其中穿越迷宫。有图片告诉你发生了什么。在您做出每个决定后,图片都会发生变化。在向左或向右输入后更改图片时出现问题。每次尝试加载图片时,我都会收到 Uncaught TypeError: Cannot set property 'src' of null 错误消息。Java 说问题发生在第 66 行

Here is my code:

这是我的代码:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Brian.Bosco-Hazey Maze Adventure</title>
<script type="application/javascript">
var flag;
var output;
var name;
var n1 = "what is your name?"
var s1 = "You wake up in a spooky maze. You must find your way out or this place will become your grave. You can go <b>Left</b> or <b>Right</b>?";
var s2 = "You come across a wizard at a fork in the maze. He gives you a magic sword to defend yourself with. You can either go <b>Left</b> or <b>Right</b>?";
var e1 = "You encounter a monster. you attempt to escape, but it pounces on you and tears off your flesh. You have died. GAME OVER. Type restart to try again";
var e2 = "You come across a dead end, looks like you have to go back the way you came. When you turn around you step on a trap on the floor and the whole tunnel collapses on you. You did not survive. Please type restart to try again."
var e3 = "While walking down this long hallway, you come across a giant monster guarding the exit door. You take the sword the wizard gave you and you slay the beast. You go through the door and exit the maze. Congradulations you have beaten the game! Type restart to play again. ";
var done = "What are you doing? You are still dead. Type restart to start again";
var myImage;
var newImage;





function init(){
 output = document.getElementById('output');
 output.innerHTML="";
 flag="n1";
 texter(n1);
 name = "";
 document.body.style.background="white";
}
function texter(newText){
 console.log(newText+" from texter");
 var oldText = document.getElementById('output').innerHTML;
 document.getElementById('output').innerHTML= newText;//+" "+oldText;
}
function Imger(newImage){
 document.getElementById('img1').src = newImage;
 document.getElementById('img2').src = newImage;

}
function game(){
 var input = document.getElementById('input').value;
 console.log("the input is "+input);
 if(input=="restart"){
  init();
 }else{
  if(flag=="n1"){
   name = input;
   flag="s1";
   texter("hello "+name+" "+s1);
   document.getElementById('img1').src = "hooded man.jpg";
   output.style.color="blue";
  }else if(flag=="s1"){
   //ask c1
   if(input=="right"){
    flag="s2";
    texter(s2);

    
     
    document.body.style.background="green";
   }else if(input=="left"){
    texter(e1);
    flag ="e1";
    document.getElementById('img2').src = "last scene.jpg";
   
   }else{
    texter("error");
   }
  }else if(flag=="s2"){
   //ask c2
   if(input=="right"){
    flag="e2";
    texter(e2);
    
    document.body.style.background="red";
   }else if(input=="left"){
    texter(e3);
    flag ="e3";
    document.body.style.background="yellow";
   }else{
    texter("error");
   }
  }else if(flag=="e1"){
   //e1
   texter(done);
  }else if(flag=="e2"){
   //e2
   texter(done);
  }else if(flag=="e3"){
   //e3
   texter(done);
  }
 }
}
</script>
</head>
<body onLoad="init()">
<img src="start.jpg" id="img1" width="500" height="500">


<p id="output">this is the start text</p>
<input type="text" id="input">
<input type="button" value="submit" onClick="game()" ">
</body>
</html>

回答by Nicolas Albert

document.getElementById('img2').src = newImage;

There is no id="img2"in the HTML, so document.getElementById('img2') == null.

id="img2"HTML 中没有,所以document.getElementById('img2') == null.

May you can add one under img1:

你可以在下面添加一个img1

<img src="start.jpg" id="img1" width="500" height="500">
<img src="start.jpg" id="img2" width="500" height="500">

OR you can remove the JS line:

或者您可以删除 JS 行:

document.getElementById('img2').src = newImage;

And also:

并且:

document.getElementById('img2').src = "last scene.jpg";

Or change it to:

或者改成:

document.getElementById('img1').src = "last scene.jpg";

回答by Jonathan J. Pecany

although I won't be much help since this was asked 3 years ago by that script should actually be after the img and the img should be behind it and than it should possibly work. I would say that you have no id's of img2 but someone already pointed it out and if his suggestion doesn't work that do my suggestion.

尽管我不会提供太多帮助,因为 3 年前该脚本曾问过这个问题,实际上应该在 img 之后,而 img 应该在它后面,而且它应该可以工作。我会说你没有 img2 的 id,但有人已经指出了,如果他的建议不起作用,那就做我的建议。