admin管理员组文章数量:1406937
I have an array of images
images:[
{image: 'img/img01.jpg'},
{image: 'img/img02.jpg'},
]
I want to make them as links:
images:[
{<a href="#"><img src="img/img01.jpg"/></a>},
{<a href="#"><img src="img/img02.jpg"/></a>},
]
And got a syntax error. What is the correct way, pls.
I have an array of images
images:[
{image: 'img/img01.jpg'},
{image: 'img/img02.jpg'},
]
I want to make them as links:
images:[
{<a href="#"><img src="img/img01.jpg"/></a>},
{<a href="#"><img src="img/img02.jpg"/></a>},
]
And got a syntax error. What is the correct way, pls.
Share Improve this question asked Jul 18, 2012 at 7:45 AliceAlice 6334 gold badges12 silver badges25 bronze badges 04 Answers
Reset to default 2You have to use strings in your array, like this:
images = [
'<a href="#"><img src="img/img01.jpg"/></a>',
'<a href="#"><img src="img/img02.jpg"/></a>'
];
for(var i=0;i<images.length;i++)
images[i].image='<a href="#"><img src="'+images[i].image+'"/></a>';
Will convert your array to
{images:[
{image:'<a href="#"><img src="img/img01.jpg"/></a>'},
{image:'<a href="#"><img src="img/img02.jpg"/></a>'},
]}
From
{images:[
{image: 'img/img01.jpg'},
{image: 'img/img02.jpg'},
]}
var images = [{image: '<a href="#"><img src="img/img01.jpg"/></a>'},
{image: '<a href="#"><img src="img/img02.jpg"/></a>'}
];
Use native method forEach. ;)
images.forEach(function(item){
item.image = '<a href="#"><img src="'+item.image+'"/></a>';
});
Preview - http://jsfiddle/NJ6Ck/
本文标签: javascriptHow to create an array of images as linksStack Overflow
版权声明:本文标题:javascript - How to create an array of images as links? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744358182a2602410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论