如何使用 jQuery 動態地用 div 包裝和開啟 HTML 控制元件?
若要包裝 html 控制元件,請使用 wrap() 方法;若要開啟 html 控制元件,請使用 unwrap() 方法。
示例
你可以嘗試執行以下程式碼,以使用 jQuery 動態地用 div 包裝和開啟 html 控制元件。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button1").click(function(){
$("p").wrap("<div></div>");
});
$("#button2").click(function(){
$("p").unwrap();
});
});
</script>
<style>
div {
background-color: gray;
}
</style>
</head>
<body>
<p>This is demo text.</p>
<p>This is another text.</p>
<button id="button1">Wrap</button>
<button id="button2">Unwrap</button>
</body>
</html>
廣告宣傳
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP