jQuery 中 jQuery.replaceAll() 和 jQuery.replaceWith() 方法有什麼區別?
jQuery.replaceAll()
replaceAll( selector ) 方法用指定的 selector 匹配到的元素替換匹配到的元素。
以下是此方法使用到的所有引數的描述:
- selector − 要查詢並用匹配元素替換的元素。
示例
您可以嘗試執行以下程式碼來學習如何使用 jQuery.replaceAll() 方法。
<html>
<head>
<title>jQuery replaceAll() method</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$('<div class = "div"></div>').replaceAll( this );
});
});
</script>
<style>
.div {
margin:10px;
padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class = "div" style = "background-color:blue;">ONE</div>
<div class = "div" style = "background-color:green;">TWO</div>
<div class = "div" style = "background-color:red;">THREE</div>
</body>
</html>jQuery.replaceWith()
replaceWith(content) 方法用指定的 HTML 或 DOM 元素替換所有匹配的元素。這將返回剛剛被替換並已從 DOM 中移除的 jQuery 元素。
以下是此方法使用到的所有引數的描述:
- content − 用來替換匹配元素的內容。
示例
您可以嘗試執行以下程式碼來學習如何使用 jQuery.replaceWith() 方法。
<html>
<head>
<title>jQuery replaceWith() method</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$(this).replaceWith( ('<div class = "div"></div>') );
});
});
</script>
<style>
.div {
margin:10px;
padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class = "div" style = "background-color:blue;">ONE</div>
<div class = "div" style = "background-color:green;">TWO</div>
<div class = "div" style = "background-color:red;">THREE</div>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP