Jquery:查找文本并替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8146648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:06:31  来源:igfitidea点击:

Jquery: Find Text and replace

jquery

提问by william

<div id="id1">
 <p>
   apple
 </p>
 <p>
   ball
 </p>
 <p>
   cat
 </p>
 <p>
   dogsss
 </p>
</div>

How Do I change dogsssto dollsssusing jquery?

我如何更改dogsssdollsss使用jquery

回答by Doug Owings

You can try

你可以试试

$('#id1 p').each(function() {
    var text = $(this).text();
    $(this).text(text.replace('dog', 'doll')); 
});

http://jsfiddle.net/2EvGF/

http://jsfiddle.net/2EvGF/

You could use instead .html()and/or further sophisticate the .replace()call according to your needs

您可以根据需要使用.html()和/或进一步完善.replace()呼叫

回答by Joseph Marikle

$("#id1 p:contains('dog')").html("doll");

that'll do it.

就可以了。

http://jsfiddle.net/pgDFQ/

http://jsfiddle.net/pgDFQ/

回答by Chamika Sandamal

try this,

尝试这个,

$('#id1').html($('#id1').html().replace('dogsss','dollsss'));

working sample

工作样本

回答by John Hartsock

$('p:contains("dogsss")').text('dollsss');

回答by sandeep kumar

Warningstring.replace('string','new string')not replaced all character. You have to use regax replace.

警告string.replace('string','new string')未替换所有字符。您必须使用 regax 替换。

For exp: I have a sting old string1, old string2, old string3and want to replace oldto new

对于 exp:我有一个旧的字符串 1、旧的字符串 2、旧的字符串 3并且想将旧的替换 为新的

Then i used.

然后我用了。

    var test = 'old string1, old string2, old string3';

   //***** Wrong method **********
    test = test.replace('old', 'new'); // This  will replaced only first match not all
    Result: new string1, old string2, old string3

  //***** Right method **********
    test = test.replace(/([old])+/g, 'new'); // This  will replaced all match 
    Result: new string1, new string2, new string3

回答by Doc Kodam

More specific:

更加具体:

$("#id1 p:contains('dog')").text($("#id1 p:contains('dog')").text().replace('dog', 'doll'));

回答by Joanna Avalos

I was looking for something similar and I use code that Doug Owings posted, but my text had some brtags and the code was erasing it.

So I use this: ( Just note that I replaced .text() to .html() )

我正在寻找类似的东西,我使用 Doug Owings 发布的代码,但我的文本有一些br标签,代码正在删除它。

所以我用这个:(请注意我将 .text() 替换为 .html() )

Text:

文本:

< p class = "textcontent" > 
     Here some text replace me 
     < br > here an other text
     < br > here is more text 
< /p>

JS:

JS:

$('.textcontent').each(function() {

   var text = $(this).html();
   $(this).html(text.replace('replace me', 'I love this text')); 

});

Also if you want to edit several text create an array:

此外,如果您想编辑多个文本,请创建一个数组:

var replacetext = {
    "Text 0": "New Text 0",
    "Text 1": "New Text 1",
    "Text 2": "New Text 2",
    "Text 3": "New Text 3",
    "Text 4": "New Text 4"
};

$.each(replacetext, function(txtorig, txtnew) {
    var text = $('#parentid').html();
    $('#parentid').html(text.replace(txtorig, txtnew)); 
});

回答by Chen Deng-Ta

Joanna Avalosanswer is better, Just note that I replaced .text()to .html(), otherwise, some of the html elements inside that will be destroyed.

Joanna Avalos 的回答更好,请注意我替换.text().html(),否则,其中的一些 html 元素将被破坏。

回答by Chris

How to change multiple"dogsss" to "dollsss":

如何将多个“dogsss”更改为“dollsss”:

$('#id1 p').each(function() {
var text = $(this).text();
// Use global flag i.e. g in the regex to replace all occurrences
$(this).text(text.replace(/dog/g, 'doll'));
});

https://jsfiddle.net/ashjpb9w/

https://jsfiddle.net/ashjpb9w/

回答by Vishal Thakkar

Change by id or class for each tag

按 id 或 class 更改每个标签

$("#id <tag>:contains('Text want to replaced')").html("Text Want to replaced with");


$(".className <tag>:contains('Text want to replaced')").html("Text Want to replaced with");