【发布时间】:2015-10-12 23:54:22
【问题描述】:
我一直在创建一个“购物网站”。我有允许我将信息从 javascript 对象添加到 html 的代码。一切都被添加,除了图像,它作为文本返回。这是我的代码:
<div id= "book1">
<div id= "twilight" class= "book product">
<div class= "name"> </div>
<div class= "catagory"></div>
<div class= "price"></div>
<div class= "description"></div>
<div class= "picture" src= ""></div>
<div class= "sellingPoints"></div>
</div>
<script>
var book1 = {
name: "Twilight",
catagory: ["Book", "Teen Romance", "Teen Paranormal"],
description: "Girl meets boy. Boy turns out to be vampire. Girl falls
in love with boy. Boy struggles with her love. A-mazing.",
pictureURL: "image URL",
price: 15.99,
sellingPoints:["Forbidden Love", "Love Triangle"]
}
$('#book1 .name').text(book1.name);
$('#book1 .catagory').text(book1.catagory);
$('#book1 .description').text(book1.description);
$('#book1 .picture').text(book1.pictureURL);
$('#book1 .price').text(book1.price);
$('#book1 .sellingPoints').text(book1.sellingPoint);
</script>
这大致就是我所拥有的(所有内容都在它们各自的标签中。我没有在此处包含它们)。文本放置在应该放置的位置,但我不知道如何将图像 url 放置在 src 中,因此它显示为实际图像,而不仅仅是 txt url。
有没有办法做到这一点,使其落在 src 中?还是有其他方法?
【问题讨论】:
-
我不想假设您的代码中有错字;你确实意识到你可以用 4 个空格来格式化代码,而且你不需要使用 inline-formatting(`),对吧?如果你不修复它可能会让一些人失望(我可以为你修复它,或者其他人当然可以)。
-
您还需要使用
<img>标签而不是<div>标签。将<div class= "picture" src= ""></div>更改为<img class="picture" src=""></img>。 Thom-x 下面的回答似乎是对的。 -
哦,这是我关于stackoverflow的第一个问题。我仍然习惯于格式化,并且在我尝试找出问题的格式时可能会搞砸一段时间。感谢您编辑 Thom-X!
标签: jquery html image append src