jQuery - 在加载正文后查找和替换文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2349446/
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-26 13:18:17  来源:igfitidea点击:

jQuery - Find and replace text, after body was loaded

jqueryfindreplace

提问by coffeemonitor

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

我从其他人那里得到了一些惊人的帮助,关于用 jquery 查找和替换文本。

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

下面的代码将找到单词:“主题:”并将其替换为“名称:”

$("*").each(function () { 
   if ($(this).children().length == 0) { 
      $(this).text($(this).text().replace('Subject:','Name:')); 
   } 
});

And this works wonderfully.

这非常有效。

The only thing I'm running into issues with is replacing text that is loaded after the page loads.

我遇到的唯一问题是替换页面加载后加载的文本。

I do have some javascript functions that are displaying data from the server, but only after the page has loaded all elements. For example, a user selects a value from a dropdown that initiates an event to load a list of products from the database.

我确实有一些 javascript 函数显示来自服务器的数据,但只有在页面加载所有元素之后。例如,用户从下拉列表中选择一个值,该值启动一个事件以从数据库加载产品列表。

I format some of those products like this:

我像这样格式化其中一些产品:

Granny Smith Apples Price: x.xx per pound Nutritional facts....

史密斯奶奶苹果价格:每磅 x.xx 营养成分......

I will only want to find a replace the word "Price:", and possibly replace it with "Cost:".

我只想找到一个替换词“价格:”,并可能用“成本:”替换它。

But as I mentioned, that data has not been loaded yet. And only displays after the user selects "Granny Smith Apples" from the dropdown menu.

但正如我所提到的,该数据尚未加载。并且仅在用户从下拉菜单中选择“Granny Smith Apples”后显示。

Is this a limit I have to live with?

这是我必须忍受的限制吗?

采纳答案by icktoofay

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

您可以尝试将事件附加到ajaxStop事件以及加载时:

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

回答by Otto Allmendinger

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

$(document).ready()像这样从回调中调用您的函数

$(document).ready(function() { replace_stuff(); } );

回答by Ziad

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