jquery Add () method usage instance _jquery

Source: Internet
Author: User

This example describes the use of the Add () method in jquery. Share to everyone for your reference. The specific analysis is as follows:

This function adds an element that matches an expression in a matching element.
The result returned by the Add () function will always be sorted in the order in which the elements appear in the HTML document, not simply added.

Grammar:

Grammar one:

Copy Code code as follows:
$ (selector). Add (Expr,context)

Parameters Describe
Expr Optional. The selector string used to match the element.
Context Optional. As a set of DOM elements to find, a document, or a JQuery object.

Instance:

Example one:

Adds a span element to the matching elements and sets their font color to green.

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "Author" content= "http://www.jb51.net/"/>
<title> Cloud-dwelling community </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>
<body>
<div>
<ul>
<li>
<p> I am p</p>
</li>
<span> I am span</span>
</ul>
<span> I am span</span> </div>
</body>

Example two:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "Author" content= "http://www.jb51.net/"/>
<title> Cloud-dwelling community </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>
<body>
<div>
<ul>
<li>
<p> I am p</p>
</li>
<span> I am span</span>
</ul>
<span> I am span</span> </div>
</body>

Syntax Two:

Copy Code code as follows:
$ (selector). Add (Element)

parameter list:
Parameters Describe
Element Optional. The DOM element or jquery element that was added .

Instance:

Example one:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "Author" content= "http://www.jb51.net/"/>
<title> Cloud-dwelling community </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>
<body>
<div>
<ul>
<li>
<p> I am p</p>
</li>
<span id= "MySpan" > I am span</span>
</ul>
<span> I am span</span> </div>
</body>

Example two:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "Author" content= "http://www.jb51.net/"/>
<title> Cloud-dwelling community </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>
<body>
<div>
<ul>
<li>
<p> I am p</p>
</li>
<span id= "MySpan" > I am span</span>
</ul>
<span> I am span</span> </div>
</body>

Syntax Three:

Copy Code code as follows:
$ (selector). Add (HTML)

parameter list:
Parameters Describe
Html Optional. The HTML code fragment that was added .

Instance:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "Author" content= "http://www.jb51.net/"/>
<title> Cloud-dwelling community </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> newly added </span>"). length);
})
</script>
<body>
<div>
<ul>
<li>
<p> I am p</p>
</li>
</ul>
</body>

I hope this article will help you with your jquery programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.