Get the App
SLTechnology News&Howtos  ›  Development  › 

How jquery replaces nodes

Shulou Source: shulou.com Published: 2022-05-31 19:48:49 09月21日 Update

This article mainly introduces jquery how to replace the node related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe everyone will have a harvest after reading this jquery how to replace the node article, let's take a look at it.

jquery node replacement method: 1, use replaceWith(), syntax "$(A).replaceWith(B)", can be used to replace node B node;2, use replaceAll(), syntax "$(A).replaceAll(B)", can be used to replace node B node.

This tutorial operates on Windows 7, jquery version 1.10.2, Dell G3 PC.

In jQuery, if we want to replace a node element, we do so using the replaceWith() and replaceAll() methods.

Method 1: Use replaceWith()

In jQuery, we can use the replaceWith() method to replace the selected element with another element.

Grammar:

$(A).replaceWith(B)

Replace A with B. Note: Both A and B must contain HTML tags.

Examples:

$(function() { $("button").click(function() { $("p").replaceWith("Hello world! ") }) })

This is a paragraph.

This is another paragraph.

Replace p node with div node

Method 2: Use replaceAll()

In jQuery, both replaceAll() and replaceWith() function similarly, replacing one element with another, but their operands are reversed.

syntax

$(A).replaceAll(B)

Replace B with A.

Examples:

$(function() { $("button").click(function() { $("Hello world! ").replaceAll("p"); }) })

This is a paragraph.

This is another paragraph.

Replace p node with div node

About "jquery how to replace nodes" This article introduces the content here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "how to replace nodes with jquery." If you still want to learn more knowledge, please pay attention to the industry information channel.

Tags: Nodes methods elements paragraphs grammar this is content knowledge examples articles similar two values functions easy to operate tutorials articles easy to understand more tags Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Redmi macOS Shulou Technology