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 0
Add a ment  | 

4 Answers 4

Reset to default 2

You 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