2016-09-21 182 views
1

我想創建一個表,並更換TD不只是第一個內部的所有實例。在這種情況下,請替換td標籤中的所有「/」,而不是第一個。查找文本的所有實例和替換所有

這裏是我的代碼:

HTML

<table border="1"> 
     <tr><td class="abc">apple</td></tr> 
     <tr><td class="abc">ball</td></tr> 
     <tr><td class="abc">cat</td></tr> 
     <tr><td class="abc">dog/ss/s</td></tr> 
    </table> 

jQuery的

$('.abc').each(function() { 
    var xyz = $(this).text(); 
    $(this).text(xyz.replace('/', 'puppy')); 
}); 

這裏是一個工作示例:Fiddle

請幫幫忙!

+2

[替換字符串中出現的所有的JavaScript(http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript) –

回答

4

使用正則表達式與全球g

$('.abc').each(function() { 
    var xyz = $(this).text(); 
    $(this).text(xyz.replace(/\//g, 'puppy')); 
}); 
+0

大的可能的複製!這工作!謝謝! – rashmeePrakash

相關問題