替换dom元素中的所有字符串
我有一块像这样的DOM
我想用jquery替换所有’0000’出现而不获取每个元素。
这可能吗?
我试过这个:
elem = $('#risposta-campione').text() // how convert dom to string?! elem.replace('0000', 'hello');
没有解决方案: – /
使用$('#risposta-campione').html().replace('0000', 'hello');
strings = $('#risposta-campione').html(); strings = strings.replace(/0000/g,"hello");
这将取代所有0000
的问题hello
。
只需使用html()函数
elem = $('#risposta-campione').html()
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
是的,它不会像你使用直接元素那样高效,但它可以在一行中完成:
$("#risposta-campione").html($("#risposta-campione").html().replace("0000", "your-new-text"));