使用jQuery查找并替换页面上的多个单词 [英] Use jQuery to find and replace multiple words on the page

查看:155
本文介绍了使用jQuery查找并替换页面上的多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
查找并替换超过1个单词?

Possible Duplicate:
Find and Replace more than 1 word?

我想使用jQuery替换特定单词的所有实例.例如,我想将我的班级"替换为我的水平",将我的班级"替换为我的水平".我也想将大学"更改为大学"

I want to use jQuery to replace all instances of specific words. For example, I want to replace "My Classes" to "My Levels" and "My Class" to "My Level". I also want to change "college" to "university"

推荐答案

您可以尝试以下方法:

var dictionary= {
"My Classes":"My Levels",
"My Class":"My Level",
"college":"university"
};

$("body *").each( function(){
    for( var ptrn in dictionary){
        $(this).text( $(this).text().replace(new RegExp(ptrn ,"g"), dictionary[ptrn] ) );
    }
});

这篇关于使用jQuery查找并替换页面上的多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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