2011-04-15 89 views
1

我有一块DOM的这样替换所有的字符串中的DOM元素

<table style="display:none;" id="risposta-campione"> 
<tr> 
<td> 
    <label>Risposta</label><textarea id="risposta_0000" name="risposta_0000" cols="35" rows="2"></textarea> 
    <button type="button" onclick="cancellaRispostaDomanda('0000')" class="cancella-risposta"></button> 
    <button type="button" onclick="aggiungiRispostaDomanda('0000')" class="aggiungi-risposta"></button> 
</td> 
</tr> 
</table> 

我想要替换所有“0000”的出现与jQuery没有得到每个元素。

这可能吗?

我尝试这样做:

elem = $('#risposta-campione').text() // how convert dom to string?! 
elem.replace('0000', 'hello'); 

无解: -/

回答

5

使用$('#risposta-campione').html().replace('0000', 'hello');

+0

太棒了!很棒!非常感谢! :-) 后代,替换全部: $('#risposta-campione tr')。html()。replace(/ 0000/g,'hello'); – Roberto 2011-04-15 15:13:37

1

只需使用HTML()函数

elem = $('#risposta-campione').html() 
5
strings = $('#risposta-campione').html(); 
strings = strings.replace(/0000/g,"hello"); 

这将全部替换出现0000hello

+0

这个问题确实要求所有实例被改变,这个答案可以做(不像其他人)。 – gonzo 2017-09-28 12:51:52

1
var text = $('#risposta-campione').html().replace(/0000/g, 'hello'); // g is case sensitive search in string 

//Or 

var text = $('#risposta-campione').html().replace(/0000/gi, 'hello'); // gi is case insensitive search in string 
0

是的,它不会是有效的,如果你是使用直接元素,但它可以在一个行完成:

$("#risposta-campione").html($("#risposta-campione").html().replace("0000", "your-new-text"));