jQuery find and replace string

前端 未结 6 948
天命终不由人
天命终不由人 2020-11-28 22:37

I have somewhere on website a specific text, let\'s say \"lollypops\", and I want to replace all the occurrences of this string with \"marshmellows\". The problem is that I

相关标签:
6条回答
  • 2020-11-28 23:05

    Below is the code I used to replace some text, with colored text. It's simple, took the text and replace it within an HTML tag. It works for each words in that class tags.

    $('.hightlight').each(function(){
        //highlight_words('going', this);
        var high = 'going';
        high = high.replace(/\W/g, '');
        var str = high.split(" ");
        var text = $(this).text();
        text = text.replace(str, "<span style='color: blue'>"+str+"</span>");
        $(this).html(text);
    });
    
    0 讨论(0)
  • 2020-11-28 23:17

    You could do something like this:

    $("span, p").each(function() {
        var text = $(this).text();
        text = text.replace("lollypops", "marshmellows");
        $(this).text(text);
    });
    

    It will be better to mark all tags with text that needs to be examined with a suitable class name.

    Also, this may have performance issues. jQuery or javascript in general aren't really suitable for this kind of operations. You are better off doing it server side.

    0 讨论(0)
  • 2020-11-28 23:17

    You could do something this way:

    $(document.body).find('*').each(function() {
        if($(this).hasClass('lollypops')){ //class replacing..many ways to do this :)
            $(this).removeClass('lollypops');
            $(this).addClass('marshmellows');
        }
        var tmp = $(this).children().remove(); //removing and saving children to a tmp obj
        var text = $(this).text(); //getting just current node text
        text = text.replace(/lollypops/g, "marshmellows"); //replacing every lollypops occurence with marshmellows
        $(this).text(text); //setting text
        $(this).append(tmp); //re-append 'foundlings'
    });
    

    example: http://jsfiddle.net/steweb/MhQZD/

    0 讨论(0)
  • 2020-11-28 23:22

    You could do something like this:

    HTML

    <div class="element">
       <span>Hi, I am Murtaza</span>
    </div>
    


    jQuery

    $(".element span").text(function(index, text) { 
        return text.replace('am', 'am not'); 
    });
    
    0 讨论(0)
  • 2020-11-28 23:22

    Why you just don't add a class to the string container and then replace the inner text ? Just like in this example.

    HTML:

    <div>
        <div>
            <p>
               <h1>
                 <a class="swapText">lollipops</a>
               </h1>
            </p>
            <span class="swapText">lollipops</span>
        </div>
    </div>
    <p>
       <span class="lollipops">Hello, World!</span>
       <img src="/lollipops.jpg" alt="Cool image" />
    </p>
    

    jQuery:

    $(document).ready(function() {
        $('.swapText').text("marshmallows");
    });
    
    0 讨论(0)
  • 2020-11-28 23:23
    var string ='my string'
    var new_string = string.replace('string','new string');
    alert(string);
    alert(new_string);
    
    0 讨论(0)
提交回复
热议问题