Posted in Javascript onJanuary 08, 2015
本文实例讲述了jQuery中add()方法用法。分享给大家供大家参考。具体分析如下:
此函数在匹配元素中添加与表达式匹配的元素。
add()函数返回的结果将始终以元素在HTML文档中出现的顺序来排序,而不再是简单的添加。
语法:
语法一:
$(selector).add(expr,context)
参数 | 描述 |
expr | 可选。用于匹配元素的选择器字符串。 |
context | 可选。作为待查找的 DOM 元素集、文档或 jQuery 对象。 |
实例:
实例一:
将span元素添加的匹配元素中去,并将它们的字体颜色设置为绿色。
<!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="https://3water.com/" /> <title>三水点靠木</title> <style type="text/css"> ul { width:200px; height:200px; border:1px solid blue; } div { 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> <div> <ul> <li> <p>我是p</p> </li> <span>我是span</span> </ul> <span>我是span</span> </div> </body> </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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="https://3water.com/" /> <title>三水点靠木</title> <style type="text/css"> ul { width:200px; height:200px; border:1px solid blue; } div { 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> <div> <ul> <li> <p>我是p</p> </li> <span>我是span</span> </ul> <span>我是span</span> </div> </body> </html>
语法二:
$(selector).add(element)
参数列表:
参数 | 描述 |
element | 可选。被添加的DOM元素或者jQuery元素。 |
实例:
实例一:
<!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="https://3water.com/" /> <title>三水点靠木</title> <style type="text/css"> ul { width:200px; height:200px; border:1px solid blue; } div { 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> <div> <ul> <li> <p>我是p</p> </li> <span id="myspan">我是span</span> </ul> <span>我是span</span> </div> </body> </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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="https://3water.com/" /> <title>三水点靠木</title> <style type="text/css"> ul { width:200px; height:200px; border:1px solid blue; } div { 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> <div> <ul> <li> <p>我是p</p> </li> <span id="myspan">我是span</span> </ul> <span>我是span</span> </div> </body> </html>
语法三:
$(selector).add(html)
参数列表:
参数 | 描述 |
html | 可选。被添加的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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="https://3water.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> <div> <ul> <li> <p>我是p</p> </li> </ul> </body> </html>
希望本文所述对大家的jQuery程序设计有所帮助。
jQuery中add()方法用法实例
- Author -
shichen2014声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@