jQuery如果div包含此文本,请替换文本的该部分 [英] jQuery if div contains this text, replace that part of the text
本文介绍了jQuery如果div包含此文本,请替换文本的该部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
就像标题中所说的那样,我想替换div中文本的特定部分.
Like the title says, I want to replace a specific part of the text in a div.
结构如下:
<div class="text_div">
This div contains some text.
</div>
例如,我只想将包含"替换为大家好". 我找不到解决方案.
And I want to replace only "contains" with "hello everyone", for example. I can't find a solution for this.
推荐答案
您可以使用 text
方法并通过使用本机 String.prototype.replace
方法执行修改后的文本的函数替换:
You can use the text
method and pass a function that returns the modified text, using the native String.prototype.replace
method to perform the replacement:
$(".text_div").text(function () {
return $(this).text().replace("contains", "hello everyone");
});
这是一个工作示例.
这篇关于jQuery如果div包含此文本,请替换文本的该部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文