jQuery wrapAll()的应用实例
wrapAll()方法是jQuery内置的一个方法,当指定的元素将被包裹在所有选择的元素中时使用。
语法:
$(selector).wrapAll(wrap_element)
参数:该方法接受一个参数wrap_element,这是必须的。这个参数用于指定哪个元素被包裹在所选元素周围。
返回值:该方法返回选定的元素和wrapAll()方法所做的指定修改。
下面的例子说明了jQuery中的wrapAll()方法。
示例:
<!DOCTYPE html>
<html>
<head>
<title>The wrapAll Method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!-- jQuery code to show the working of this method -->
<script>
(document).ready(function() {
("button").click(function() {
$("p").wrapAll("<div></div>");
});
});
</script>
<style>
div {
background-color: lightgreen;
padding: 20px;
width: 200px;
font-weight: bold;
height: 60px;
border: 2px solid green;
}
</style>
</head>
<body>
<!-- click on this paragraph and see the change -->
<p>Welcome to GeeksforGeeks!<br><br>
<button>Click Here!</button></p>
</body>
</html>
输出: