jquery replacement Dom Node method _jquery

Source: Internet
Author: User

The examples in this article describe the way jquery replaces DOM nodes. Share to everyone for your reference. The specific analysis is as follows:

If you are replacing a node, jquery provides the appropriate method, namely ReplaceWith () and ReplaceAll ().

The ReplaceWith () method replaces all matching elements with the specified HTML or DOM element.

This example JQuery code:

<script type= "Text/javascript" >
//<![ cdata[
$ (function () {
 $ (#btn_1). Click (function () {
  $ (". Nm_p"). ReplaceWith (' <p class= ' nm_p ' > Welcome to visit Www.jb51.net</p> '); 
 }
 $ ("#btn_2"). Click (function () {
  $ (". Nm_p"). ReplaceWith (' <p class= nm_p ' title= "Welcome to cloud-dwelling communities" > Welcome to Cloud-dwelling communities </p > '); 
  Same implementation: $ (' <p class= ' nm_p ' > Welcome to Www.jb51.net</p> '). ReplaceAll (". nm_p"); 
 })
});
]]>
</script>

You can also use another method in jquery, ReplaceAll (), which is the same as the ReplaceWith () method, but reverses the replacewith () operation, using the following jquery code to achieve the same function:

Copy Code code as follows:
$ (' <p class= ' nm_p ' > Welcome to Www.jb51.net</p> '). ReplaceAll (". nm_p");

Both of these jquery code implement the node substitution effect.

PS: If you have already bound an event for an element before replacing it, the previously bound event will disappear along with the replaced element, and you will need to rebind the event on the new element.

I hope this article will help you with your jquery programming.

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.