Home >Web Front-end >JS Tutorial >Example of using jQuery tag replacement function replaceWith()
This article mainly introduces the use examples of jQuery tag replacement function replaceWith(). Use replaceWith to replace tags in templates, and it can also be implemented Multi-language website, friends in need can refer to
replaceWith simple use
In jQuery, there is a powerful replacement function replaceWith(), It is very simple to use, for example:
The page has the following p tags
Replace all p tags with "##"
$('p').replaceWith('##');
Result after execution:
Replace tag
Using this replaceWith, we can replace all The p tag is replaced with the b tag, and the content remains unchanged:
$('p').each(function(){ $(this).replaceWith('<b>'+$(this).html()+'</b>'); });
The result
This is the replacement!
Multi-language websites can easily use this function to complete
If you are developing a multi-language website, you can even use this feature, for example, when you need to translate Add an i tag to the text, and then traverse the translation replacement.
Suppose the page dom structure is as follows:
We need to translate the text in the i tag on the page. Those with i tags on the page are Apple ,computer. So we need a translation library:
var translate = { '苹果' : 'apple', '电脑' : 'PC' };
Then I can perform translation replacement like this
$('i').each(function(){ $(this).replaceWith(translate[$(this).html()]); });
The effect after execution :
Page effect:
The above is the detailed content of Example of using jQuery tag replacement function replaceWith(). For more information, please follow other related articles on the PHP Chinese website!