这篇文章主要介绍了jquery中wrapall()方法用法,实例分析了wrapall()方法的功能、定义及匹配元素的使用技巧,需要的朋友可以参考下
本文实例讲述了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.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>语法二:
用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.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>以上就是jQuery中wrapAll()方法用法实例详解的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号