jQuery replaceAll and methods of replaceWith

There are two alternative methods may perform operations on the label, replaceWith () and the replaceAll () method in jQuery: 

replaceWith (): to replace all matching elements or specified HTML DOM elements.

replaceAll (selector): replace all selector matching element with a matching element.

In the following code, using the first tag selector acquires span, b, i tag, then call The replaceWith () method to replace h3 tag.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script src="js/jquery-3.3.1.js"></script>
	</head>
	<body>
		<span>123</span>
		<b>123</b>
		<i>123</i>
		
		<script>
			$("span,b,i").replaceWith("<h3>HelloWorld</h3>");
		</script>
	</body>
</html>

 And this code is used to call the replaceAll h3 tag () method, passing jQuery object span, b, i is the label in the parameter list. Two programs operating results are as follows:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script src="js/jquery-3.3.1.js"></script>
	</head>
	<body>
		<span>123</span>
		<b>123</b>
		<i>123</i>
		
		<script>
			$("<h3>HelloWorld</h3>").replaceAll("span,b,i");
		</script>
	</body>
</html>

Published 99 original articles · won praise 93 · views 5214

Guess you like

Origin blog.csdn.net/DangerousMc/article/details/103016600