实例 jQuery 使用函数指定包裹每个选定元素的内容

x
 
<!DOCTYPE html>
<html>
<head>
<script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").wrap(function(){
        return "<div></div>"
    });
  });
});
</script>
<style>
div {
  background-color: yellow;
  padding: 10px;
}
</style>
</head>
<body>
<p>This is a paragraph.</p>
<button>Wrap a div element around the p element</button>
</body>
</html>
                    

输出结果