如何使用 jQuery 動態地用一個 div 包裝和解包 HTML 控制元件?
要包裝 html 控制元件,請使用 wrap() 方法,使用 unwrap() 方法解包 html 控制元件。
示例
你可以嘗試執行以下程式碼,使用 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>
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP