Use jQuery to find and replace multiple words on t

2019-09-21 10:18发布

Possible Duplicate:
Find and Replace more than 1 word?

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"

1条回答
Fickle 薄情
2楼-- · 2019-09-21 10:45

You may try something like this:

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] ) );
    }
});
查看更多
登录 后发表回答