【问题标题】:Image from array is not appended to <div> id数组中的图像未附加到 <div> id
【发布时间】:2017-03-24 18:30:34
【问题描述】:

我有一个图像数组以及内容页面何时加载。数组中的一张图片将附加到 &lt;div&gt; id 并显示出来。

var GameQuestion = ["A?", "B?", "C?"];

var GameAnswer = [
  ["lib/True.png", "lib/False.png"],
  ["lib/True.png", "lib/False.png"],
  ["lib/True.png", "lib/False.png"]
];

var answerList,
  random_Question;

random_Question = Math.floor(Math.random() * GameQuestion.length);

$("#question").html(GameQuestion[random_Question]);

answerList = GameAnswer[random_Question];

$("#Answer_1").html(answerList[0]);
$("#Answer_2").html(answerList[1]);
<!-- Original Question -->
<div id="question" style="position:absolute; z-index:6; top:750px; left:160px; margin:auto; color:#FFFFFF; font-size:30px; width:800px; text-align: center;"></div>

<!-- Answer-Original-Choice List -->
<div id="Answer_1" class="answers" style="position:absolute; z-index:6; top:1006px; left:224px; margin:auto; color:#FFFFFF; font-size:25px; width:750px;"></div>
<div id="Answer_2" class="answers" style="position:absolute; z-index:6; top:1186px; left:224px; margin:auto; color:#FFFFFF; font-size:25px; width:750px;"></div>

当内容页面加载时,我只能看到问题,但是,应该附加和显示图像的部分。它只显示文件路径名。

因此,如果内容页面加载问题:A?,则 2 图像 id 应加载并显示真假图像。但是此时,我只看到 img id "Answer_1" 和 "Answer_2" 的文件路径名 "lib/True.png" 和 "lib/False.png"。

那么,有什么问题吗?因为我需要 img id 来显示图像而不是显示文件路径名。

请帮忙。谢谢你

【问题讨论】:

  • 您的&lt;img&gt;...具体在哪里?
  • @Gothdo,对不起,它本来是一个
    ,我太昏昏沉沉了

标签: javascript jquery html arrays image


【解决方案1】:

您必须创建一个带有src 属性的&lt;img&gt;。有几种方法可以做到这一点,其中创建 HTML 字符串是一种非常简单的方法:

$("#Answer_1").html("<img src='" + answerList[0] + "'/>")

这将创建一个类似"&lt;img src='lib/False.png'/&gt;" 的字符串。 jQuery html 方法会将其放入您的 #Answer_1 div 中,浏览器会将其解析为 HTML。

更好的方法可能是已经创建了&lt;img&gt; 标记并且只更新src 属性。您的 HTML 将类似于:

<div><img id="Answer_1" /></div>

带js:

$("#Answer_1").attr("src", answerList[0]);

现在,浏览器不必每次执行此操作时都删除/注入新图像。

(请注意,图像在 sn-p 中不起作用)

var GameQuestion = ["A?", "B?", "C?"];

var GameAnswer = [
  ["lib/True.png", "lib/False.png"],
  ["lib/True.png", "lib/False.png"],
  ["lib/True.png", "lib/False.png"]
];

var answerList,
  random_Question;

random_Question = Math.floor(Math.random() * GameQuestion.length);

$("#question").html(GameQuestion[random_Question]);

answerList = GameAnswer[random_Question];

$("#Answer_1").html("<img src='" + answerList[0] + "'/>");
$("#Answer_2").html("<img src='" + answerList[1] + "'/>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Original Question -->
<div id="question" style="position:absolute; z-index:6; top:750px; left:160px; margin:auto; color:#FFFFFF; font-size:30px; width:800px; text-align: center;"></div>

<!-- Answer-Original-Choice List -->
<div id="Answer_1" class="answers" style="position:absolute; z-index:6; top:1006px; left:224px; margin:auto; color:#FFFFFF; font-size:25px; width:750px;"></div>
<div id="Answer_2" class="answers" style="position:absolute; z-index:6; top:1186px; left:224px; margin:auto; color:#FFFFFF; font-size:25px; width:750px;"></div>

【讨论】:

  • 对不起,我的意思是把它附加到
    id 标签
猜你喜欢
  • 2011-06-25
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 2015-10-12
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
相关资源
最近更新 更多