Jquery - Add img title to thumbnails
I'm having problems grabbing the title tag from an image in a slider and inserting it somewhere else on my page. It's only grabbing the first title from the first image and not the subsequent ones.
My script:
var imgTitle = $('.nivoSlider img').attr('title')
$('a.nivo-control').append('<p>' + (imgTitle) + '</p>');
I know I have to use .each someplace but I don'开发者_JAVA百科t know where.
Thanks
Use .map()
to get an array of titles. Then use the index parameter in your call to .each()
to get the corresponding title:
var titles = $('.nivoSlider img').map(function() {
return this.title;
}).get();
$('a.nivo-control').each(function(i) {
$(this).append("<p>" + titles[i] + "</p>");
});
Try this for example:
$('.nivoSlider img').each( function() {
alert($(this).attr('title'));
});
I think you will get all the images title attributes
精彩评论