JavaScript 自动更改img src

JavaScript Change img src automatically

问题

我的项目快要完成了,但我还有一个问题无法解决。我需要让图像 src 在提示框关闭时自动更改。这就是我的意思。

例子

假设我有这个代码。

HTML

<button id="begin" onClick="javascript:fight();">Begin</button>
<img id="scenario" src="http://www.thegaminghideout.com/school/stage1.png">

JAVASCRIPT

function fight()  {
  var img = document.getElementById("scenario");
  var start = document.getElementById("begin");
  img.src = "http://www.thegaminghideout.com/school/map.jpg"; /* This changes it when the function is called */
  var decision1 = function()  {
    var decision = prompt("What do you do now? ATTACK, SLEEP, or PASS?").toUpperCase();
    if(decision === "ATTACK")  {
      attack();
    }
    if(decision === "SLEEP")  {
      sleep();
    }
    if(decision === "PASS")  {
      skip();
    }
  }

问题

如何在提示对话框关闭时自动更改img的src?或者,检查它是否打开?

我试过的

注意事项

function fight() {
  var img = document.getElementById("scenario");
  var start = document.getElementById("begin");
  img.src = "http://www.thegaminghideout.com/school/map.jpg"; /* This changes it when the function is called */
  decision1();
  var decision1 = function() {
    var decision = prompt("What do you do now? ATTACK, SLEEP, or PASS?").toUpperCase();
    if (decision === "ATTACK") {
      attack();
    }
    if (decision === "SLEEP") {
      sleep();
    }
    if (decision === "PASS") {
      skip();
    }
  }
}
<img id="scenario" src="http://www.thegaminghideout.com/school/stage1.png">
<button id="begin" onClick="javascript:fight();">Test</button>

http://jsfiddle.net/d8sxxykj/

function fight()  {

  var start = document.getElementById("begin");
  var decision1 = function()  {
    var img = document.getElementById("scenario");
    var decision = prompt("What do you do now? ATTACK, SLEEP, or PASS?").toUpperCase();
    if(decision === "ATTACK")  {
    img.src = "http://www.thegaminghideout.com/school/map.jpg";
    attack();
    }
    if(decision === "SLEEP")  {
    img.src = "something.jpg";
    sleep();
    }
    if(decision === "PASS")  {
    img.src = "something.jpg";
    skip();
    }
}  

试试,

$("#scenario").attr("src", "http://www.thegaminghideout.com/school/stage1.png");

而不是

img.src = "http://www.thegaminghideout.com/school/stage1.png";

希望对您有所帮助!