jQuery if div contains this text, replace that part of the text

后端 未结 5 1589
一生所求
一生所求 2020-11-29 20:05

Like the title says, I want to replace a specific part of the text in a div.

The structure looks like this:

This d
5条回答
  •  孤街浪徒
    2020-11-29 20:39

    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"); 
    });​​​​​
    

    Here's a working example.

提交回复
热议问题