2012-03-28 107 views
-2

可能重复:
Find and Replace more than 1 word?使用jQuery查找并在页面上替换多个单词

我想使用jQuery来代替特定单词的所有实例。例如,我想将“我的课程”替换为“我的课程”和“我的课程”改为“我的课程”。我也想“学院”改为“大学”

+0

搜索这个论坛给我: http://stackoverflow.com/questions/2349446/jquery -find-and-replace-text-after-body-was-loaded and http://stackoverflow.com/questions/2349138/jquery-find-and-replace-text-without-element-id – pikand 2012-03-28 13:39:48

+0

I已经尝试过其他例子,但它们都只是替换一个单词(X与Y),我需要替换多组单词(X与Y,A与B,1与2等) – 2012-03-28 13:46:15

回答

2

您可以尝试这样的事:

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])); 
    } 
}); 
相关问题