我想获取值<span>Expertise discovery</span>
并为href添加标题,如下所示:<a href="discovery.html" title="Expertise discovery">
<li class="tooltip-parent">
<a href="discovery.html">
<span>
<img src="discovery.png"/>
</span>
</a>
<div class="tooltip">
<span>Expertise discovery</span>
</div>
</li>
答案 0 :(得分:2)
您可以尝试这样的事情:
//find the span inside tooltip and get its contents
var title = $(".tooltip").find("span").text();
//select the a tag with href set to discovery.html and add the title attribute to it.
$('[href="discovery.html"]').attr("title", title);
答案 1 :(得分:1)
$('.tooltip-parent').each(function(){
var $this = $(this);
$this.children('a').attr('title', $this.find('.tooltip span').text())
})
演示:Fiddle
答案 2 :(得分:1)
这将对您和所有其他具有相同标记结构的图像进行所需的更改。
$('.tooltip').each(function(){
var val = $(this).text();
$(this).parent().find('a').prop('title',val);
});
答案 3 :(得分:0)
$("[href='discovery.html']").attr("title", $(".tooltip > span").text());
OR
$(".tooltip-parent > a").attr("title", $(".tooltip > span").text());
答案 4 :(得分:0)
使用此代码
$('a[href="discovery.html"]').attr("title",$(".tooltip-parent .tooltip").find('span').text(););