如何将图像填充为链接,这些链接都作为JSON对象存储在数组中
How to populate an image as a link which are both stored in an array as JSON objects?
请点击我的JSFiddle链接查看我正在尝试做的代码。正如您将注意到的,我将标题、图像和链接存储为JSON对象,但是当您运行我的代码时,它不会像在HTML文件中指定的那样将标题和图像显示为链接以及在属性中指定的链接。我研究这个问题已经有一段时间了,但我还是搞不明白,虽然这看起来应该是显而易见的事情。有人有什么想法、建议或解决方案吗?我尝试将JavaScript中的输出设置为",但这不起作用。
HTML:<a id="food" class="thumbnail" target="_blank">
<img width="200" class="img-shadow">
<hr>
<h3></h3>
</a>
JavaScript: var data={"food":[
{
"title":"Pumpkin Spice Bread Recipe",
"image":"img/bread.jpg",
"link":"http://willowbirdbaking.com/2011/09/18/pumpkin-spice-pull-apart-
bread-with-butter-rum-glaze/"
}
]}
var output="<ul>";
// Create a variable to temporarily store the data
for (var i in data.food) {
output+="<li>" + data.food[i].title + data.food[i].image + data.food[i].link + "</li>";
}
output+="</ul>";
// Once we go through all of the elements in the array,
// we use the output variable to populate the placeholder div.
document.getElementById("food").innerHTML=output;
JSFiddle
提前感谢,非常感谢任何一点帮助。
您必须使用<img />
标记输出图像。你的错误是你只是输出图像链接。
使用图片链接标签:var bob = "some text <img src='"+data.food[i].image+"' />";
试试这个:
var data={"food":[
{
"title":"Pumpkin Spice Bread Recipe",
"image":"img/bread.jpg",
"link":"http://willowbirdbaking.com/2011/09/18/pumpkin-spice-pull-apart-
bread-with-butter-rum-glaze/"
}
]}
var output="<ul>";
// Create a variable to temporarily store the data
for (var i in data.food) {
output+="<li>" + data.food[i].title + "<img src='"+data.food[i].image+"' /> " + data.food[i].link + "</li>";
}
output+="</ul>";
// Once we go through all of the elements in the array,
// we use the output variable to populate the placeholder div.
document.getElementById("food").innerHTML=output;
如果你想让你的图像成为链接,你必须把你的图像放入<a></a>
标签,像这样:
for (var i in data.food) {
output+="<li><a title='" + data.food[i].title + "' href='"+data.food[i].link+"'><img title='" + data.food[i].title + "' src='"+data.food[i].image+"' /></li>";
}
最终代码:
var data={"food":[
{
"title":"Pumpkin Spice Bread Recipe",
"image":"img/bread.jpg",
"link":"http://willowbirdbaking.com/2011/09/18/pumpkin-spice-pull-apart-
bread-with-butter-rum-glaze/"
}
]}
var output="<ul>";
// Create a variable to temporarily store the data
for (var i in data.food) {
output+="<li><a title='" + data.food[i].title + "' href='"+data.food[i].link+"'>" + data.food[i].title + "<img title='" + data.food[i].title + "' src='"+data.food[i].image+"' /></li>";
}
output+="</ul>";
// Once we go through all of the elements in the array,
// we use the output variable to populate the placeholder div.
document.getElementById("food").innerHTML=output;
和jsfiddle。
在<a>
标签内。你只需要一个href属性就可以让它看起来像一个真正的链接。
相关文章:
- 使用jquery延迟对象链接多个ajax调用
- 什么是“;原型;通过JavaScript中的Object Literal Notation创建的对象的链接
- 从JSON对象创建具有超链接的HTML表
- 如何从 MVC4 链接调用 Javascript 方法(错误 JavaScript 运行时错误:对象不支持此操作)
- 如何使用 HTML/JavaScript 为对象添加链接
- 当图像和链接是 json 对象属性的值时,它们不起作用
- 检查链接对象形式中未定义属性的最佳实践是什么
- javascript'='用作链接对象
- 如何通过html链接将特定对象传递给函数
- 是否可以使用HTML从任意字符串创建Jquery对象,例如var$newlink=$('<a>新链接
- 对象.创建和链接对象
- 在指令链接中访问 ngModel 的父对象
- Javascript:如何在不复制但链接的情况下将另一个对象 B 的方法混合到我的对象 A
- AngularJS 指令:使用包含/编译链接集合中的对象
- 循环访问链接对象,如何避免重复
- 高亮显示背景窗格大于链接对象且没有填充的链接
- 链接对象时如何使用相同的方法
- Xlxs.js的“l"单元格超链接对象选项工作
- 在ASP中为超链接对象的属性添加撇号等字符.净VB
- 如何从构造函数返回一个数组,并成功地在Javascript中链接对象