使用document.write()在页面上显示图像 [英] Showing images on a page with document.write()

查看:2111
本文介绍了使用document.write()在页面上显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用document.write()在我的javascript文件中发布图像。问题是,当我尝试它时,图像总是会破碎。这是代码。我写的标签错了吗?我已经在非JavaScript页面上测试了这些图像,它们似乎显示得很好。

I'm trying to post an image when in my javascript file with document.write(). The problem is that when I try it, the image always comes up broken. Here is the code. Am I writing the tag wrong? I've tested the images on non-javascript pages and they seem to show up fine.

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);


推荐答案

你差不多了,我想你只需要用斜杠替换反斜杠:'< img src =images / orna.jpg>'。这是一个演示(点击运行直到获胜): http://jsfiddle.net/wared/d9wVC/

You almost got it, I think you just have to replace backslashes with slashes : '<img src="images/orna.jpg">'. Here is a demo (click "run" until win) : http://jsfiddle.net/wared/d9wVC/.

这篇关于使用document.write()在页面上显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆