【问题标题】:Jquery: Find Text and replaceJquery:查找文本并替换
【发布时间】:2011-11-16 04:16:36
【问题描述】:
<div id="id1">
 <p>
   apple
 </p>
 <p>
   ball
 </p>
 <p>
   cat
 </p>
 <p>
   dogsss
 </p>
</div>

如何使用jquerydogsss 更改为dollsss

【问题讨论】:

  • 不,Tkz for all,但你误解了我的意思。sss 是一个我不知道它到底是什么的文本。但我想将dog 更改为doll
  • 类似于 C# 替换string temp="tasting";temp=temp.Replace("tast","test"); sth 这样。

标签: jquery


【解决方案1】:

你可以试试

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

http://jsfiddle.net/2EvGF/

您可以改用.html() 和/或根据您的需要进一步完善.replace() 调用

【讨论】:

  • 仅替换每个 p 中的第一次出现,而不是每个!
  • 如果你需要插入像&amp;nbsp;这样的html实体,你必须使用.html()而不是.text()
【解决方案2】:
$("#id1 p:contains('dog')").html("doll");

这样就可以了。

http://jsfiddle.net/pgDFQ/

【讨论】:

  • 如果p 包含狗在吠怎么办?
  • @Vikrant 你是绝对正确的。我提供的这个解决方案是不完整的,它是在我开发生涯的早期编写的。我过去曾考虑将其删除,但我一直保留它,因为它包含缩小选择器contains() 并且有足够的声誉来引起人们的注意。我喜欢接受的答案。我赞成它,但这个答案没有被删除的唯一原因是因为我认为它仍然为这个问题增加了价值。但是,我完全愿意就是否应该删除它来接受其他意见。
  • @JosephMarikle 您可以编辑答案并为其添加更多价值以涵盖其他情况。 :)
  • let p = $("#id1 p:contains('dog')"); p.text(p.text().replace(/\bdog\b/g, 'doll'));
【解决方案3】:

试试这个,

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

working sample

【讨论】:

  • @JohnHartsock:查看工作示例。它正在更新。希望你错过设置的 html 部分:)
  • 当多个 li 存在文本 dogsss 时完全失败
【解决方案4】:

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

对于经验: 我有刺 old string1, old string2, old string3 并想替换 old

然后我用了。

    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

【讨论】:

  • 纯 JavaScript 总是更好!
【解决方案5】:
$('p:contains("dogsss")').text('dollsss');

【讨论】:

    【解决方案6】:

    更具体:

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

    【讨论】:

    • 使用.html()而不是.text()来保留HTML标签。
    【解决方案7】:

    如何将多个“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/

    【讨论】:

      【解决方案8】:

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

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

      文字:

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

      JS:

      $('.textcontent').each(function() {
      
         var text = $(this).html();
         $(this).html(text.replace('replace me', 'I love this text')); 
      
      });
      

      另外,如果你想编辑多个文本创建一个数组:

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

      【讨论】:

      • 如何将它用于具有相同id或相同类的多个记录?
      【解决方案9】:

      Joanna Avalos回答比较好,注意我把.text()换成了.html(),不然里面的一些html元素会被破坏掉。

      【讨论】:

        【解决方案10】:

        按每个标签的 id 或类更改

        $("#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");
        

        【讨论】:

          【解决方案11】:

          试试这个

          $("#id1 p:contains('dogsss')").replaceWith("dollsss");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-10-25
            • 2020-08-23
            • 1970-01-01
            • 1970-01-01
            • 2013-04-16
            • 1970-01-01
            • 2012-05-22
            • 1970-01-01
            相关资源
            最近更新 更多