jQuery中add()方法用法实例教程
本文实例讲述了jquery中add()方法用法。分享给大家供大家参考。具体分析如下:
此函数在匹配元素中添加与表达式匹配的元素。
add()函数返回的结果将始终以元素在html文档中出现的顺序来排序,而不再是简单的添加。
语法:
语法一:
代码如下:
$(selector).add(expr,context)
实例:
实例一:
将span元素添加的匹配元素中去,并将它们的字体颜色设置为绿色。
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.cnblogs.com/" />
<title>博客园</title>
<style type="text/css">
ul
{
width:200px;
height:200px;
border:1px solid blue;
}
p
{
height:300px;
}
</style>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").add("span").css("color","green")
})
</script>
</head>
<body>
<p>
<ul>
<li>
<p>我是p</p>
</li>
<span>我是span</span>
</ul>
<span>我是span</span> </p>
</body>
</html>
实例二:
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.cnblogs.com/" />
<title>博客园</title>
<style type="text/css">
ul
{
width:200px;
height:200px;
border:1px solid blue;
}
p
{
height:300px;
}
</style>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").add("span","ul").css("color","green")
})
</script>
</head>
<body>
<p>
<ul>
<li>
<p>我是p</p>
</li>
<span>我是span</span>
</ul>
<span>我是span</span> </p>
</body>
</html>
语法二:
代码如下:
$(selector).add(element)
参数列表:
实例:
实例一:
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.cnblogs.com/" />
<title>博客园</title>
<style type="text/css">
ul
{
width:200px;
height:200px;
border:1px solid blue;
}
p
{
height:300px;
}
</style>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").add(document.getelementbyid("myspan")).css("color","green")
})
</script>
</head>
<body>
<p>
<ul>
<li>
<p>我是p</p>
</li>
<span id="myspan">我是span</span>
</ul>
<span>我是span</span> </p>
</body>
</html>
实例二:
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.cnblogs.com/" />
<title>博客园</title>
<style type="text/css">
ul
{
width:200px;
height:200px;
border:1px solid blue;
}
p
{
height:300px;
}
</style>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").add($("#myspan")).css("color","green")
})
</script>
</head>
<body>
<p>
<ul>
<li>
<p>我是p</p>
</li>
<span id="myspan">我是span</span>
</ul>
<span>我是span</span> </p>
</body>
</html>
语法三:
代码如下:
$(selector).add(html)
参数列表:
实例:
代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="https://www.cnblogs.com/" />
<title>博客园</title>
<script type="text/javascript" src="mytest/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
alert($("p").add("<span>新添加的</span>").length);
})
</script>
</head>
<body>
<p>
<ul>
<li>
<p>我是p</p>
</li>
</ul>
</body>
</html>
上一篇: python爬取前程无忧热门城市职位信息
下一篇: MIUI 11即将发布:现已开启内测申请