jQuery replaceWith() 方法如何工作?
replaceWith( content ) 方法用指定的 HTML 或 DOM 元素替換所有匹配的元素。它會返回剛剛被替換的 jQuery 元素,該元素已從 DOM 中移除。
以下是對此方法所用所有引數的說明,−
- content - 用於替換匹配元素的內容。
示例
你可以嘗試執行以下程式碼以瞭解如何使用 replaceWith() 方法
<html>
<head>
<title>jQuery replaceWith() method</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/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