This article mainly introduces the usage of wrapAll() method in jQuery. The function, definition and usage skills of matching elements are analyzed with examples. Friends in need can refer to the following
The examples of this article are explained Learn how to use the wrapAll() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:
This method wraps all matching elements with a single element.
Although this method has similar functions to the wrap() method, there is a big difference. The wrap() method will match each matching element once.
Syntax 1:
Wrap the matching element with the specified DOM element.
The code is as follows:
$(selector).wrapAll(elem)
Parameter list:
Parameter Description |
elem |
The DOM element used to wrap the target. |
Example:
The code is as follows: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="http://www.jb51.net/" />
<head>
<title>脚本之家</title>
<style type="text/css">
p
{
width:200px;
height:200px;
border:1px solid blue;
margin-top
:10px;
}
</style>
<script type="text/
javascript
" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(
document
).ready(function(){
$("button").click(function(){
$("p").wrapAll(document.getElementById("content"));
});
});
</script>
</head>
<body>
<p>我是个段落</p>
<p>我是个段落</p>
<p id="content"></p>
<button>用p包裹每个段落</button>
</body>
</html> Copy after login Syntax 2: Wrap the matching elements with html tag code .
The code is as follows: $(selector).wrapAll(html) Copy after login Parameter list:
Parameters | Description | ##elem | The DOM element used to wrap the target. |
Example: The code is as follows:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="http://www.jb51.net/" />
<head>
<title>脚本之家</title>
<style type="text/css">
.content
{
width:200px;
height:200px;
border:1px solid blue;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").wrapAll("<p class='content'></p>");
});
});
</script>
</head>
<body>
<p>这是个段落</p>
<p>这是个段落</p>
<button>用p包裹每个段落</button>
</body>
</html> Copy after login
|
The above is the detailed content of Detailed explanation of usage examples of wrapAll() method in jQuery. For more information, please follow other related articles on the PHP Chinese website!