jQuery - 在加载正文后查找和替换文本 [英] jQuery - Find and replace text, after body was loaded

查看:23
本文介绍了jQuery - 在加载正文后查找和替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 "Name:"

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

这非常有效.

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

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

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

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.

我将其中一些产品格式化如下:

I format some of those products like this:

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

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

我只想找到一个替代词价格:",并可能将其替换为成本:".

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

但正如我所提到的,该数据尚未加载.并且只有在用户从下拉菜单中选择Granny Smith Apples"后才会显示.

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.

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

Is this a limit I have to live with?

推荐答案

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

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

这篇关于jQuery - 在加载正文后查找和替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆