JQuery replaceWith () replace tag function usage

Source: Internet
Author: User

This article will introduce you to the usage of the jQuery replaceWith () Replacement tag function. If you need to know the replaceWith function, refer to it.

Jquery replaces the content of the selected element with the replaceWith () method

ReplaceWith, replace Element

This article introduces how to use the replaceWith () method in jquery. If you have any questions, please refer to it. I hope this article will help you.

The replaceWith () method replaces the content of the selected element with other content.


Let's first look at an instance.

The Code is as follows: Copy code

<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // DTDHTML 4.01 Transitional //" http://www.w3.org/TR/html4/loose.dtd ">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Inserttitle here </title>
<Script type = "text/javascript" src = "scripts/jquery-1.7.2.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Button"). click (function (){
$ ("P"). replaceWith ("<B> hello </B> ");
// Replace the content in the p element with "<B> hello </B>"
});
});
</Script>
</Head>
 

<Body>
<P> 1111111111111111111111 </p>
<Button> click </button>
</Body>
</Html>

Many of my friends may not understand it. I will introduce it one by one.

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 "#"

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

Result After execution:

 

Replace tags

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

?
1 2 3 $('p').each(function(){     $(this).replaceWith('<b>'+$(this).html()+'</b>'); });

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:

?
1 2 3 4 var translate = {     'Apple' : 'apple',     'Pc' : 'PC' };

Then I can perform translation replacement in this way.

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

Effect after execution:

Page effect:

ReplaceWith and replaceAll

Similarities: both of them can be used to find and replace

Difference: the writing method is different. I didn't find out what functions they have.

The Code is as follows: Copy code

<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Strong"). replaceAll ($ ("div"); // use the strong label to replace all div labels.
// $ ("Div"). replaceWith ($ ("strong"); // similar to the above, there is no difference, and the writing method is different
});
</Script>

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.