jQuery - Find and replace text, after body was loaded

后端 未结 3 761
忘掉有多难
忘掉有多难 2020-12-05 05:31

I received some amazing help from others, concerning finding and replacing text with jquery.

The code below will find the word: \"Subject:\" and replace it with \"Na

相关标签:
3条回答
  • 2020-12-05 06:06

    Call your function from the $(document).ready() callback like this

    $(document).ready(function() { replace_stuff(); } );
    
    0 讨论(0)
  • 2020-12-05 06:19

    You could try attaching an event to the ajaxStop event as well as on load:

    function replaceText() {
        var jthis = $(this);
        $("*").each(function() { 
            if(jthis.children().length==0) { 
                jthis.text(jthis.text().replace('Subject:', 'Name:')); 
            } 
        });
    }
    $(document).ready(replaceText);
    $("html").ajaxStop(replaceText);
    
    0 讨论(0)
  • 2020-12-05 06:19

    The function below works perfectly for me:

    function replaceText(selector, text, newText, flags) {
      var matcher = new RegExp(text, flags);
      $(selector).each(function () {
        var $this = $(this);
        if (!$this.children().length)
           $this.text($this.text().replace(matcher, newText));
      });
    }
    

    Here's a usage example:

    function replaceAllText() {
      replaceText('*', 'Subject:', 'Name:', 'igm');
    }
    
    $(document).ready(replaceAllText);
    $('html').ajaxStop(replaceAllText);
    
    0 讨论(0)
提交回复
热议问题