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?或者,检查它是否打开?
我试过的
- 将
img.src =
"http://www.thegaminghideout.com/school/stage1.png";
放在
fight()
函数
- 尝试这样的检查:
if(fight() === true) { img.src =<br>
"http://www.thegaminghideout.com/school/stage1.png"; }
- 添加了一个 'refresh' 函数,但它破坏了代码。
注意事项
- 我知道我在示例中的 JavaScript 可能看起来是错误的,
因为我语法错误或者没有
attack()
、sleep()
或
skip()
函数。好吧,这只是一个例子,所以我不是 posting
我的整个游戏代码在这里,大约有 1000 行。
- 我将 post 上面提供的代码片段和一个 JSFiddle
配合实际项目配合完整代码更好
故障排除。
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>
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";
希望对您有所帮助!
问题
我的项目快要完成了,但我还有一个问题无法解决。我需要让图像 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?或者,检查它是否打开?
我试过的
- 将
img.src = "http://www.thegaminghideout.com/school/stage1.png";
放在fight()
函数 - 尝试这样的检查:
if(fight() === true) { img.src =<br> "http://www.thegaminghideout.com/school/stage1.png"; }
- 添加了一个 'refresh' 函数,但它破坏了代码。
注意事项
- 我知道我在示例中的 JavaScript 可能看起来是错误的,
因为我语法错误或者没有
attack()
、sleep()
或skip()
函数。好吧,这只是一个例子,所以我不是 posting 我的整个游戏代码在这里,大约有 1000 行。 - 我将 post 上面提供的代码片段和一个 JSFiddle 配合实际项目配合完整代码更好 故障排除。
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>
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";
希望对您有所帮助!