Replace word in

in

using jquery

前端 未结 8 1797
无人及你
无人及你 2021-01-18 11:54

I have a document with the following structure:

This is some text.

相关标签:
8条回答
  • 2021-01-18 12:30

    This is an overkill but anyway:

    function replaceNodeText() {
        if (this.nodeType === 3) {
            this.nodeValue = this.nodeValue.replace(replaceNodeText.find, replaceNodeText.replace);
        } else {
            $(this).contents().each(replaceNodeText);
        }
    }
    replaceNodeText.find = "some";
    replaceNodeText.replace = "my";
    $("#notice").contents().each(replaceNodeText);
    

    This function will preserve any html present inside the specified element. For example it will work on this HTML:

    <div id="notice" class="box generalbox">
        <p>This is<br>some text.</p>
        <p>This is so<br>me text.</p>
        <p>This is <b>some</b> text.</p>
    </div>
    

    And produce the following output:

    <div id="notice" class="box generalbox">
        <p>This is<br>my text.</p>
        <p>This is so<br>me text.</p>
        <p>This is <b>my</b> text.</p>
    </div>
    

    Demo here

    0 讨论(0)
  • 2021-01-18 12:31
    var text = $("#notice p").text()
    text = text.replace("some", "My");
    $("#notice p").text(text);
    
    0 讨论(0)
  • 2021-01-18 12:43

    Try this

    $('#notice').html().replace("some", "my");
    
    0 讨论(0)
  • 2021-01-18 12:43

    Salmans answer works good, but if you have more than 1 word in a paragraph it wouldn't be replaced. So use this instead: (with a regex that matches globally)

    function replaceNodeText() {
        if (this.nodeType === 3) {
            this.nodeValue = this.nodeValue.replace(replaceNodeText.regex, replaceNodeText.replace);
        } else {
            $(this).contents().each(replaceNodeText);
        }
    }
    replaceNodeText.regex = /some/g;
    replaceNodeText.replace = "my";
    $("#notice").contents().each(replaceNodeText);
    

    Or use this:

    (I just changed .replace(x,y) to .split(x).join(y), this is way faster than replace(), see here)

    function replaceNodeText() {
        if (this.nodeType === 3) {
            this.nodeValue = this.nodeValue.split(replaceNodeText.find).join(replaceNodeText.replace);
        } else {
            $(this).contents().each(replaceNodeText);
        }
    }
    replaceNodeText.find = "some";
    replaceNodeText.replace = "my";
    $("#notice").contents().each(replaceNodeText);
    

    Demo on jsfiddle

    0 讨论(0)
  • 2021-01-18 12:49

    Go a head and try this solution:

    newtext = $("#notice p").text().replace("some", "My"); 
    $("#notice p").text(newtext);
    
    0 讨论(0)
  • 2021-01-18 12:53

    Read http://api.jquery.com/text/#text-functionindex--text

    $("#notice p").text(function (_, ctx) {
        return ctx.replace("some", "My");
    });
    

    or

    $("#notice p").text($("#notice p").text().replace("some", "My"));
    

    or

    var  p_tag = $("#notice p");
    p_tag.text(p_tag.text().replace("some", "My"));
    
    0 讨论(0)
提交回复
热议问题