2009-01-27 35 views
1

我的目標是使每個外部鏈接的標題等於它的href屬性。如何使鏈接標題與使用jQuery的URL相同?

我的問題是,我該如何使title變量可用於attr函數?

$('a').filter(function() { 

    var title= $(this).attr('href'); 
    return this.hostname && this.hostname !== location.hostname; 

}) 
    .removeAttr('target') 
    .attr('rel', 'external') 
    .attr('title', title); 

$('a[rel="external"]').click(function() { 
    window.open($(this).attr('href')); 
    return false; 
}); 

我覺得我莫名其妙地把它向後,答案是attr(key, fn)

回答

3
$('a').each(function() { 
    var href = $(this).attr('href'); 
    if(this.hostname && this.hostname !== location.hostname) { 
     $(this).removeAttr('target') 
       .attr('rel', 'external') 
       .attr('title', href) 
       .click(function() { 
        window.open(href); 
        return false; 
       }); 
    } 
}); 
+0

謝謝!有沒有辦法將這與點擊功能結合起來? – meleyal 2009-01-27 18:23:35

相關問題