使用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] ) );
}
});