wrapAll()于wrap()是不同的,wrap()为每一个匹配的元素都包裹一次。而wrapAll()则是把每一个匹配的元素合成相邻元素再进行全部包裹。
经过HTML代码段:
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".btn1").click(function(){
$("p").wrapAll("<div></div>");
});
});
</script>
<style type="text/css">
div{background-color:yellow;}
</style>
</head>
<body>
A
<p>This is a paragraph.</p>
B<p>This is another paragraph.</p>
C<button class="btn1">用一个 div 包裹所有段落</button>
</body>
</html>
----------------------后前台界面如下
A
This is a paragraph.
B
This is another paragraph.
C
--------------------当点击了button控件后前台界面会如下:
A
This is a paragraph.
This is another paragraph.
BC