手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >jQuery中wrapAll()方法用法实例
jQuery中wrapAll()方法用法实例
摘要:本文实例讲述了jQuery中wrapAll()方法用法。分享给大家供大家参考。具体分析如下:此方法将所有匹配的元素用单个元素包裹起来。此方法...

本文实例讲述了jQuery中wrapAll()方法用法。分享给大家供大家参考。具体分析如下:

此方法将所有匹配的元素用单个元素包裹起来。

此方法与wrap()方法虽然功能类似,但是有着很大区别。wrap()方法会把每一个匹配的元素都匹配一次。

语法一:

用指定的DOM元素去包裹匹配元素。

复制代码 代码如下:$(selector).wrapAll(elem)

参数列表:

参数 描述
elem 用于包裹目标的DOM元素。

实例:

复制代码 代码如下:

<!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">

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)

参数列表:

参数 描述
elem 用于包裹目标的DOM元素。

实例:

复制代码 代码如下:

<!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("<div></div>");

});

});

</script>

</head>

<body>

<p>这是个段落</p>

<p>这是个段落</p>

<button>用div包裹每个段落</button>

</body>

</html>

希望本文所述对大家的jQuery程序设计有所帮助。

【jQuery中wrapAll()方法用法实例】相关文章:

JQuery中DOM事件冒泡实例分析

jQuery聚合函数实例

JQuery中attr方法和removeAttr方法用法实例

JavaScript中的small()方法使用详解

jQuery中 prop() attr()使用详解

javaScript中with函数用法实例分析

JQuery中Text方法用法实例分析

JQuery中节点遍历方法实例

JavaScript中exec函数用法实例分析

javaScript中push函数用法实例分析

精品推荐
分类导航