且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的查找和替换文本,没有元素ID

更新时间:2023-02-23 14:09:10

Takpar,你的代码也可以。它似乎停止了一些其他的工作,但只有我拉动物品。例如,当我使用 .ajax()时。不知道为什么,但这就是我碰到的原因。



$ b
 $ b $ p $  $(this).text($($) (this).text()。replace('Subject:','Name:')); 
}
});

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

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



我将这些产品的一部分格式化为:

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

我只想找到一个替换单词Price:,并且可能将其替换为Cost: 。

但是,正如我所提到的,该数据尚未加载。



是我必须忍受的限制吗?

I'm playing around with finding and replacing text.

The only problem I'm having is finding a text string, that is not attached to an element with an ID. Otherwise, it would be easy.

I'm trying something like this:

$("*").each(function () {
    $(this).html(this.html().replace('Original Text','New Text'));
});

Not working too well.
Anyone run into this before?

Also, if I have several words or phrases to find and replace, how does that affect the speed/processing power of the user's browser? Is it a memory hog?

Takpar, your code works too. It seems to stop a few other things from working, but only items I'm pulling dynamically. For example, when I'm using .ajax(). Not sure why, but that's why I ran into. I'll test more.

On a related topic, Gumbo's code works:

$("*").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.

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:

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

But as I mentioned, that data has not been loaded yet.

Is a limit I have to live with?