Use 'alt' value to dynamically set 'title'

一笑奈何 提交于 2019-12-06 05:26:10
$(function(){
   $("a").each(function(){
        $(this).attr("title", $(this).find("img").attr("alt"));
   });
});

you didn't mention exactly which <a>s you wanted to change, so this would change all <a>s with an <img> inside...

$("a>img").each(function() {
  $(this).parent().attr("title", $(this).attr("alt"))
})

With jQuery:

var a = $('a[rel=lightbox])');
var img = a.find('img');
a.attr('title', img.attr('alt'));

You can do it like this:

$(function() {
    var img = $(".post-image");
    if(img.length == 1) // meaning, at least one element was found
    {
        img.attr("title", img.attr("alt")); // pass the text in alt to title
        img.removeAttr("alt");              // remove alt
    }
});
$('a').each(function(){
    $(this).attr('title',$(this).find('img').attr('alt'));
});

Since jsFiddle.net seems to be flaky over the last day or so, you can see a demo at jsbin.

And if jsFiddle loads, a jsFiddle example.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!