jQuery中wrapAll()方法用法实例
本文实例讲述了jQuery中wrapAll()方法用法。分享给大家供大家参考。具体分析如下:
此方法将所有匹配的元素用单个元素包裹起来。
此方法与wrap()方法虽然功能类似,但是有着很大区别。wrap()方法会把每一个匹配的元素都匹配一次。
语法一:
用指定的DOM元素去包裹匹配元素。
$(selector).wrapAll(elem)
参数列表:
实例:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<metaname="author"content="https://www.nhooo.com/"/>
<head>
<title>毛票票</title>
<styletype="text/css">
div
{
width:200px;
height:200px;
border:1pxsolidblue;
margin-top:10px;
}
</style>
<scripttype="text/javascript"src="mytest/jQuery/jquery-1.8.3.js"></script>
<scripttype="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").wrapAll(document.getElementById("content"));
});
});
</script>
</head>
<body>
<p>我是个段落</p>
<p>我是个段落</p>
<divid="content"></div>
<button>用div包裹每个段落</button>
</body>
</html>
语法二:
用html标记代码包裹匹配的元素。
$(selector).wrapAll(html)
参数列表:
实例:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<metaname="author"content="https://www.nhooo.com/"/>
<head>
<title>毛票票</title>
<styletype="text/css">
.content
{
width:200px;
height:200px;
border:1pxsolidblue;
}
</style>
<scripttype="text/javascript"src="mytest/jQuery/jquery-1.8.3.js"></script>
<scripttype="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").wrapAll("<divclass='content'></div>");
});
});
</script>
</head>
<body>
<p>这是个段落</p>
<p>这是个段落</p>
<button>用div包裹每个段落</button>
</body>
</html>
希望本文所述对大家的jQuery程序设计有所帮助。