本文实例讲述了jQuery中wrapAll()方法用法。分享给大家供大家参考。具体分析如下:
此方法将所有匹配的元素用单个元素包裹起来。
此方法与wrap()方法虽然功能类似,但是有着很大区别。wrap()方法会把每一个匹配的元素都匹配一次。
语法一:
用指定的DOM元素去包裹匹配元素。
$(selector).wrapAll(elem)
参数列表:
实例:
<!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.phpstudy.net/\" />
<head>
<title>phpstudy</title>
<style type=\"text/css\">
div
{
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>
<div id=\"content\"></div>
<button>用div包裹每个段落</button>
</body>
</html>
语法二:
用html标记代码包裹匹配的元素。
$(selector).wrapAll(html)
参数列表:
实例:
<!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.phpstudy.net/\" />
<head>
<title>phpstudy</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(\"<div class=\'content\'></div>\");
});
});
</script>
</head>
<body>
<p>这是个段落</p>
<p>这是个段落</p>
<button>用div包裹每个段落</button>
</body>
</html>
希望本文所述对大家的jQuery程序设计有所帮助。
本文地址:https://www.stayed.cn/item/2560
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我