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.