替换dom元素中的所有字符串 [英] Replace all strings in dom element

查看:119
本文介绍了替换dom元素中的所有字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的DOM

I've a piece of DOM like this

<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>

我想用jquery替换所有出现的'0000',而不获取每个元素.

I want to replace all '0000' occurrences with jquery without getting each element.

这可能吗?

我尝试过:

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

无解:-/

推荐答案

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

这会将0000的所有出现替换为hello.

this will replace all the occurance of 0000 to hello.

这篇关于替换dom元素中的所有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆