Home > Web Front-end > JS Tutorial > What is the difference between replacewith and replaceAll

What is the difference between replacewith and replaceAll

清浅
Release: 2019-02-21 11:03:36
Original
4690 people have browsed it

The difference between replacewith and replaceAll in jQuery is: the former replaces the selected elements with the characters in the brackets, and the latter replaces the selected elements with the strings in the brackets

There are two in jQuery Two methods are used to specify the content of HTML or replace selected elements with elements. They are respectively the replacewith and replaceAll methods. Next, in the article, we will introduce the difference between the two, which has a certain reference effect. I hope it will be helpful to everyone. Helps.

What is the difference between replacewith and replaceAll

[Recommended tutorial: jQuery tutorial]

replacewith method

replaceWith() method is to replace the selected element with new content. It has two parameters: content, which is used to set the content to be inserted. It can be set to html or DOM elements. Another parameter is function. Function used to return the replaced content

Example: Click the button to modify the text content

<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").replaceWith("PHP中文网");
    });
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<button>点击按钮替换元素</button>
Copy after login

Rendering :

What is the difference between replacewith and replaceAll

##replaceAll method

The replaceAll method, like the replacewith method, is used to replace selected elements with new HTML elements. It also has two parameters, where content is used to set the content to be inserted, and selector is used to specify which element will be replaced.

Example: Clicking the button will change the paragraph in the p element

<script>
$(document).ready(function(){
	$("button").click(function(){
		$("<p>PHP中文网</p>").replaceAll("p");
	});
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<button>点击按钮进行替换</button>
Copy after login

Rendering:

What is the difference between replacewith and replaceAll

The difference between replacewith and replaceAll

From the above two From the example, we can see that the functions implemented between the two are the same, used to replace text content, but there are still differences in the implementation process

The biggest difference between them is the replacement of characters In the order, replacewith replaces the selected elements with the characters in the brackets, and replaceAll replaces the selected elements in the brackets with strings. And the replacement is completed, all events in the replaced element disappear

Summary: The above is the entire content of this article. I hope this article can help everyone understand the difference between replacewith and replaceAll

The above is the detailed content of What is the difference between replacewith and replaceAll. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template