JQuery tag replacement function replaceWith () Example _ jquery-js tutorial

Source: Internet
Author: User
This article describes how to use the jQuery tag replacement function replaceWith (). You can use replaceWith to replace the tags in the template and implement multilingual websites. For more information, see ReplaceWith for simple use

In jQuery, there is a powerful replacement function replaceWith (), which is very simple to use, such:

The page has the following p tags:

Replace all p tags with "#"

$('p').replaceWith('##');

Result After execution:

Replace tags

With this replaceWith, we can replace all p tags with B tags with the same content:

$('p').each(function(){    $(this).replaceWith(''+$(this).html()+'');});

Result

This is replaced!

Multilingual websites can use this function to easily complete

If you are developing a multi-language website, you can even use this feature. For example, add an I tag to the text you want to translate and then traverse the translation to replace it.

Assume that the dom structure of the page is as follows:

We need to translate the text in the I tag on the page. The I tags on the page are apple and computer. Therefore, we need a translation Library:

Var translate = {'apple': 'apple', 'pc '};

Then I can perform translation replacement in this way.

$('i').each(function(){    $(this).replaceWith(translate[$(this).html()]);});

Effect after execution:

Page effect:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.