搜索文本并用JavaScript替换

时间:2013-01-12 19:09:36

标签: javascript jquery text replace

示例我有这段代码:

<div class="description"> I make you smile. </div>

转换为:

<div class="description"> I make you think. </div>

注意:“微笑”被替换为“思考”

如何使用javascript jQuery替换它?可以改变几个单词吗?

1 个答案:

答案 0 :(得分:4)

var ele = $("div.description");                     //Let ele be target element
ele.html(ele.html().replace(/smile/gi,"think"));    //Replace the word in string,
                                                    // then put it back in.

.replace MDN

演示:http://jsfiddle.net/DerekL/FdyEH/