使用’alt’值动态设置’title’
我需要一些帮助,在我们开始之前,我知道这可能不是最佳做法,但我正在对现有网站进行一些维护,需要完成以下修复。
好的,所以我试图弄清楚如何使用jQuery或任何其他方法从我的图像中获取alt
属性,并动态覆盖我a
标签的title
属性。
任何帮助都会很棒,我在这个时刻有点失落。
$(function(){ $("a").each(function(){ $(this).attr("title", $(this).find("img").attr("alt")); }); });
$("a>img").each(function() { $(this).parent().attr("title", $(this).attr("alt")) })
使用jQuery:
var a = $('a[rel=lightbox])'); var img = a.find('img'); a.attr('title', img.attr('alt'));
你可以这样做:
$(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')); });
由于jsFiddle.net在最后一天左右看似片状,你可以在jsbin看到一个演示。
如果jsFiddle加载, 则为jsFiddle示例 。