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/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744358182a4570336.html
评论列表(0条)