【发布时间】:2020-08-01 17:50:33
【问题描述】:
我正在尝试使用 document.write() 在我的 javascript 文件中发布图像。问题是当我尝试它时,图像总是出现损坏。这是代码。我写错标签了吗?我已经在非 JavaScript 页面上测试了图像,它们似乎显示得很好。
var replay = "yes";
var userChoice = prompt("Do you choose rock, paper or scissors?");
userChoice = userChoice.toLowerCase();
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
document.write("The result is a tie!");
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
document.write('<img src="images\orna.jpg\">');
} else {
document.write("Computer chose paper. Computer wins.");
}
}
if (choice1 === "paper") {
if (choice2 === "scissors") {
document.write("Computer chose scissors. Computer wins.");
} else {
document.write("Computer chose rock. Player wins.");
}
}
if (choice1 === "scissors") {
if (choice2 === "paper") {
document.write("Computer chose paper. Player wins.");
} else {
document.write("Computer chose scissors. Computer wins.");
}
}
};
compare(userChoice, computerChoice);
【问题讨论】:
-
您的某些图片的完整 URL 是什么? example.com/???
-
图像在我的闪存驱动器上的一个文件夹中。我的 html 文件比它高一级。所以一个例子是 images\rock.jpg
-
document.write("");
标签: javascript html image