2011-10-15 22 views
3

鏈接內的文本,我有以下鏈接HTML:如何更改與jQuery

<a href="#" onclick="run(1); return false;" class="link" title="Remove item">[x]</a> 

,我想使用jQuery來改變這是

<a href="#" onclick="run(1); return false;" class="link" title="Remove item">[x] Remove Item</a> 

我想保留鏈接和所有的屬性,但改變鏈接內的文字

+0

可能重複http://stackoverflow.com/questions/7415292/jquery-change-child-text) –

+0

http://api.jquery.com/text/ –

回答

7

您可以在text()函數的鏈接中獲取文本,並使用文本(String)函數將其設置爲:

$(".link").text($(".link").text() + " Remove Item"); 

如果您只想定位一個鏈接而不是所有具有.link類的元素,您可以爲鏈接指定一個唯一的ID。

+2

請注意,這會將第一個「.link」元素的文本添加到其他所有元素中。 –

2
$('.link').text($('.link').text() + 'Remove Item'); 

OR

$('.link').each(function(){ 
    $(this).text($(this).text() + 'Remove item'); 
}); 

OR

$('.link').each(function(){ 
    $(this).text($(this).text() + $(this).attr('title')); 
}); 
+0

如果只需要更改一個元素,則最好使用ID。 – Dev

0

使用:

jQuery(document).ready(
    function($) 
    { 
     if($('.link').text() == '[x]') 
     { 
      $('.link').text('[x] Remove'); 
     } 
    } 
); 
1

試試這個:

var $link = $('a[title="Remove item"]'); 
$link.text($link.text()+ ' ' + $link.attr('title')); 

更多:以下是比較明顯的,但遠不如通用:

$('a[title="Remove item"]').text('[x] Remove item'); 
1
$('.link').each(function() { 
    $(this).text($(this).text() + ' ' + $(this).attr('title')); 
}); 
-1
$("a.link").append('Remove item') 
[jQuery的變化子文本(的