- 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.jquery.com/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.jquery.com/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
廣告
