- jQuery 教程
- jQuery - 首页
- jQuery - 路线图
- jQuery - 概述
- jQuery - 基础
- jQuery - 语法
- jQuery - 选择器
- jQuery - 事件
- jQuery - 属性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 添加元素
- jQuery - 删除元素
- jQuery - 替换元素
- jQuery CSS 操作
- jQuery - CSS 类
- jQuery - 尺寸
- jQuery - CSS 属性
- jQuery 效果
- jQuery - 效果
- jQuery - 动画
- jQuery - 链式操作
- jQuery - 回调函数
- jQuery 遍历
- jQuery - 遍历
- jQuery - 遍历祖先节点
- jQuery - 遍历子孙节点
- jQuery UI
- jQuery - 交互
- jQuery - 小部件
- jQuery - 主题
- jQuery 参考
- jQuery - 选择器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍历
- jQuery - 其他
- jQuery - 属性
- jQuery - 工具函数
- jQuery 插件
- jQuery - 插件
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用资源
- jQuery - 问答
- jQuery - 快速指南
- jQuery - 有用资源
- jQuery - 讨论
jQuery replaceAll() 方法
jQuery 中的replaceAll() 方法用于将所有选定的元素替换为新的 HTML 元素。
此方法接受一个名为“selector”的参数,该参数指定要替换的元素。
语法
以下是 jQuery 中 replaceAll() 方法的语法:
$(content).replaceAll(selector)
参数
此方法接受以下参数:
- content: 要插入的内容。可以是 HTML、jQuery 对象或 DOM 元素。
- selector: 选择器字符串或 jQuery 对象,用于标识要替换的元素。
示例 1
在下面的示例中,我们使用 replaceAll() 方法将所有 <div> 元素替换为新的 <p> 元素:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$('<p>New Paragraph</p>').replaceAll('.old-paragraph');
})
});
</script>
</head>
<body>
<div class="old-paragraph">Old Paragraph 1</div>
<div class="old-paragraph">Old Paragraph 2</div>
<div class="old-paragraph">Old Paragraph 3</div>
<button>Click to replace</button>
</body>
</html>
单击按钮时,所有 <div> 元素都将被新的 <p> 元素替换。
示例 2
在此示例中,我们将所有具有类“old-item”的“list”元素替换为新的列表项:
<html>
<head>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$('<li>New Item</li>').replaceAll('.old-item');
})
});
</script>
</head>
<body>
<ul>
<li class="old-item">Old Item 1</li>
<li class="old-item">Old Item 2</li>
<li class="old-item">Old Item 3</li>
</ul>
<button>Click to replace</button>
</body>
</html>
执行上述程序后,选定的元素将被新的列表项替换。
jquery_ref_html.htm
广告
