要包装html控件,请使用wrap()和unwrap()方法解包html控件。
您可以尝试运行以下代码,以使用jQuery动态地对div的html控件进行包装和解包。
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/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>