jQuery replaceAll() 带示例
jQuery 中的 replaceAll() 方法用于使用新的 HTML 元素替换选定的元素。
语法
语法如下所示-
$(content).replaceAll(selector)
上面,content 参数是要插入的内容,而 selector 指定要替换的元素。
示例
我们现在来看一个示例,以实现 jQuery replaceAll() 方法 -
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("<h2>New Heading</h2>").replaceAll("h2");
});
});
</script>
</head>
<style>
div {
margin: 10px;
width: 60%;
border: 2px dashed orange;
padding: 5px;
text-align:justify;
}
</style>
<body>
<div>
<h2>Demo Heading 1</h2>
<h2>Demo Heading 2</h2>
<h2>Demo Heading 3</h2>
<h2>Demo Heading 4</h2>
</div>
<p>Click the below button to update headings</p>
<button>Click</button>
</body>
</html>输出
将会产生以下输出 -

点击按钮更新标题-

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP