开发者

Jquery - Add img title to thumbnails

开发者 https://www.devze.com 2023-04-07 13:32 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号