2013-07-26 43 views
0

我想獲得價值<span>Expertise discovery</span>並添加標題到href的:<a href="discovery.html" title="Expertise discovery">jquery如何獲得一個值並將其放置在其他地方?

<li class="tooltip-parent"> 
    <a href="discovery.html"> 
    <span> 
     <img src="discovery.png"/> 
    </span> 
    </a> 
    <div class="tooltip"> 
     <span>Expertise discovery</span> 
    </div> 
</li> 

回答

1

,你可以這樣做

$('.tooltip-parent').each(function(){ 
    var $this = $(this); 
    $this.children('a').attr('title', $this.find('.tooltip span').text()) 
}) 

演示:Fiddle

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); 
0
$("[href='discovery.html']").attr("title", $(".tooltip > span").text()); 

OR

$(".tooltip-parent > a").attr("title", $(".tooltip > span").text()); 
1

Demo here

這將你需要的變化,這和所有其他的圖像具有相同的標記結構。

$('.tooltip').each(function(){ 
    var val = $(this).text(); 
    $(this).parent().find('a').prop('title',val); 
}); 
0

使用此代碼

$('a[href="discovery.html"]').attr("title",$(".tooltip-parent .tooltip").find('span').text();); 
相關問題