简体   繁体   English

控制台在尝试 innerHTML 时显示 net::ERR_FILE_NOT_FOUND

[英]console showing net::ERR_FILE_NOT_FOUND when trying innerHTML

I am really new to coding and I am following this video tutorial to make a rock, paper, scissors game.我对编码真的很陌生,我正在按照这个视频教程制作一个石头、纸、剪刀游戏。 I made a JavaScript function with innerHTML, but I keep getting an error when I test it out in the console log.我用 innerHTML 制作了 JavaScript function,但是当我在控制台日志中对其进行测试时,我一直收到错误消息。 In my code, the humandiv.innerHTML works just find;在我的代码中,humandiv.innerHTML 工作只是找到; however, the botdiv.innerHTML is creating the net::ERR_FILE_NOT_FOUND.但是,botdiv.innerHTML 正在创建 net::ERR_FILE_NOT_FOUND。 Any idea what is going on?知道发生了什么吗?

function rpsfrontend(humanimagechoice, botimagechoice, finalmessage) {
    var imagesdatabase = {
        "rock": document.getElementById("rock").src,
        "paper": document.getElementById("paper").src,
        "scissors": document.getElementById("scissors").src,
    }
    // lets remove all images
    document.getElementById("rock").remove();
    document.getElementById("paper").remove();
    document.getElementById("scissors").remove();

    var humandiv = document.createElement("div");
    var botdiv = document.createElement("div");
    var messagediv = document.createElement("div");

    humandiv.innerHTML = "<img src='" + imagesdatabase[humanimagechoice] + "' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(37, 50, 233, 1);'>"
    botdiv.innerHTML = "<img src='" + imagesdatabase[botimagechoice] + "' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(37, 50, 233, 1);'>" 

document.getElementById("flexboxrpsdiv").appendChild(humandiv);
document.getElementById("flexboxrpsdiv").appendChild(botdiv); }

As per your comment根据你的评论

humanimagechoice has rock as a value and therefore it worked with imagesdatabase[humanimagechoice] because imagesdatabase['rock'] holds a value. humanimagechoice具有 rock 作为值,因此它与imagesdatabase[humanimagechoice]一起使用,因为 imagesdatabase['rock'] 拥有一个值。

While botimagechoice has {message: "You Lost,": color: "red"} as a value虽然botimagechoice{message: "You Lost,": color: "red"}作为值

And you are trying to read imagesdatabase[{message: "You Lost,": color: "red"}] when you write imagesdatabase[botimagechoice] which is not a valid syntax hence you are getting the error.当您编写imagesdatabase[botimagechoice]时,您正在尝试读取imagesdatabase[{message: "You Lost,": color: "red"}] ,这不是有效的语法,因此您会收到错误消息。

You botimagechoice should be some string like rock .botimagechoice应该是一些类似于rock的字符串。

I think you are trying to pass the result of the bot and the human game as the bot image.It's going to work fine if u correct that我认为您正在尝试将机器人和人类游戏的结果作为机器人图像传递。如果您纠正它,它将正常工作

So you are trying to access the object imagesdatabase with the key {message: "You Lost,": color: "red"} ;因此,您尝试使用键{message: "You Lost,": color: "red"}访问 object 图像数据库; which it is not able to find;它无法找到; hence the error ERR_FILE_NOT_FOUND因此错误 ERR_FILE_NOT_FOUND

The usual Structure os a JSON object is as follows: JSON:{key:value}; JSON object 的常用结构如下: JSON:{key:value}; The value can be another JSON in turn meaning u can have a JSON of the following structure该值可以是另一个 JSON 依次意味着您可以具有以下结构的 JSON

const imagesdatabase = {
        "rock": document.getElementById("rock").src,
        "paper": document.getElementById("paper").src,
        "scissors": document.getElementById("scissors").src,
    }

it is always a good idea to make the json variable a consant将 json 变量设为常数总是一个好主意

from the code as per my understanding, you just need the JSON key as the output, you could do something like this:根据我的理解,从代码中,您只需要 JSON 密钥作为 output,您可以执行以下操作:

humandiv.innerHTML = "<img src='" + Object.keys(imagesdatabase[humanimagechoice])[0] + "' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(37, 50, 233, 1);'>"
botdiv.innerHTML = "<img src='" + Object.keys(imagesdatabase[botimagechoice])[0] + "' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(37, 50, 233, 1);'>" 

so the code above gives just the key of the object.所以上面的代码只给出了 object 的密钥。 frankly, I would suggest storing the key in another variable like this and using them in the code.坦率地说,我建议将密钥存储在像这样的另一个变量中并在代码中使用它们。 Here is my version with what I think are the best practices:这是我认为最佳实践的版本:

   let imageDatabase;
//function to set the imageDatabse variable
function setImageDatabase() {
  imageDatabase = {
    "rock": document.getElementById("rock").src,
    "paper": document.getElementById("paper").src,
    "scissors": document.getElementById("scissors").src,
  }
};

//function to set the inner html

function setInnerHtml(botImageChoice, humanImageChoice) {
  const humanDiv = document.createElement("div");
  const botDiv = document.createElement("div");
  const messageDiv = document.createElement("div");
  //try to follow camel casing where ever possible
  //use backtics for better readibility
  humandiv.innerHTML = `<img src=' ${imagesDatabase[humanImageChoice]}' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(37, 50, 233, 1);'>`;
  botdiv.innerHTML = `<img src=' ${imagesDatabase[botImageChoiceKey]}' height=150 width=150 style='box-shadow: 0px 10px 50px rgba(37, 50, 233, 1);'>`
  document.getElementById("flexboxrpsdiv").appendChild(humanDiv);

  document.getElementById("flexboxrpsdiv").appendChild(botDiv);

  /*you might also want to append the result of the game here*/
}
// the function to remove the images
function removeImages() {
  document.getElementById("rock").remove();
  document.getElementById("paper").remove();
  document.getElementById("scissors").remove();

}

//your actual caller function
function rpsfrontend(humanimagechoice, botimagechoice, finalmessage) {
  /*avoid using var , and try making the variables as const/let where ever possible,trust me it helps a lot in the future*/

  /* why not make this a seperate function,it is a good practise to make one function to do one job
  var imageDatabase = {
    "rock": document.getElementById("rock").src,
    "paper": document.getElementById("paper").src,
    "scissors": document.getElementById("scissors").src,
  }
  document.getElementById("rock").remove();
  document.getElementById("paper").remove();
  document.getElementById("scissors").remove();
  */

  setImageDatabase();
  removeImages();
  setInnerHtml(botImageChoice, humanImageChoice);

  /*
  botImageChoice is 'rock'
  humanImageChoice is 'paper'
  in this case the structure of the you will get

  imageDatabase[botImageChoice] as imageDatabase['rock'] with respective value

  and similaraly for the 
  imageDatabase[humanImageChoice] as imageDatabase['paper'] with respective value*/

}

//call the function here; hope this helps;:) happy coding

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM