mirror of
https://github.com/Hopiu/jquery-mobile.git
synced 2026-03-18 15:00:23 +00:00
167 lines
No EOL
7.1 KiB
HTML
167 lines
No EOL
7.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang='en'><head>
|
|
<meta charset="utf-8" /><title>test</title><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
|
|
<div data-role="page">
|
|
<div data-role="header">
|
|
<h1>.add()</h1>
|
|
</div>
|
|
<div data-role="content" data-theme="c" id="add1">
|
|
<h2 class="jq-clearfix roundTop section-title">
|
|
<span class="name">.add( selector )</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#jQuery">jQuery</a></span>
|
|
</h2>
|
|
<div class=" entry-details">
|
|
<p class="desc"><strong>Description: </strong>Add elements to the set of matched elements.</p>
|
|
<ul class="signatures">
|
|
<li class="signature" id="add-selector">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.add( selector )</h4>
|
|
<p class="arguement"><strong>selector</strong>A string containing a selector expression to match additional elements against.</p>
|
|
</li>
|
|
<li class="signature" id="add-elements">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.add( elements )</h4>
|
|
<p class="arguement"><strong>elements</strong>one or more elements to add to the set of matched elements.</p>
|
|
</li>
|
|
<li class="signature" id="add-html">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.add( html )</h4>
|
|
<p class="arguement"><strong>html</strong>An HTML fragment to add to the set of matched elements.</p>
|
|
</li>
|
|
<li class="signature" id="add-selector-context">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>.add( selector, context )</h4>
|
|
<p class="arguement"><strong>selector</strong>A string containing a selector expression to match additional elements against.</p>
|
|
<p class="arguement"><strong>context</strong>Add some elements rooted against the specified context.</p>
|
|
</li>
|
|
</ul>
|
|
<div class="longdesc">
|
|
<p>Given a jQuery object that represents a set of DOM elements, the <code>.add()</code> method constructs a new jQuery object from the union of those elements and the ones passed into the method. The argument to <code>.add()</code> can be pretty much anything that <code>$()</code> accepts, including a jQuery selector expression, references to DOM elements, or an HTML snippet.</p>
|
|
<p>Consider a page with a simple list and a paragraph following it:</p>
|
|
<pre><ul>
|
|
<li>list item 1</li>
|
|
<li>list item 2</li>
|
|
<li>list item 3</li>
|
|
</ul>
|
|
<p>a paragraph</p></pre>
|
|
<p>We can select the list items and then the paragraph by using either a selector or a reference to the DOM element itself as the <code>.add()</code> method's argument:</p>
|
|
<pre>$('li').add('p').css('background-color', 'red');</pre>
|
|
<p>Or:</p>
|
|
<pre>$('li').add(document.getElementsByTagName('p')[0])
|
|
.css('background-color', 'red');</pre>
|
|
<p>The result of this call is a red background behind all four elements.
|
|
Using an HTML snippet as the <code>.add()</code> method's argument (as in the third version), we can create additional elements on the fly and add those elements to the matched set of elements. Let's say, for example, that we want to alter the background of the list items along with a newly created paragraph:</p>
|
|
<pre>$('li').add('<p id="new">new paragraph</p>')
|
|
.css('background-color', 'red');</pre>
|
|
<p>Although the new paragraph has been created and its background color changed, it still does not appear on the page. To place it on the page, we could add one of the insertion methods to the chain.</p>
|
|
<p>As of jQuery 1.4 the results from .add() will always be returned in document order (rather than a simple concatenation).</p>
|
|
</div>
|
|
<h3>Examples:</h3>
|
|
<div id="entry-examples" class="entry-examples">
|
|
<div id="example-0">
|
|
<h4>Example: <span class="desc">Finds all divs and makes a border. Then adds all paragraphs to the jQuery object to set their backgrounds yellow.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
div { width:60px; height:60px; margin:10px; float:left; }
|
|
p { clear:left; font-weight:bold; font-size:16px;
|
|
color:blue; margin:0 10px; padding:2px; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<div></div>
|
|
|
|
<div></div>
|
|
<div></div>
|
|
<div></div>
|
|
<div></div>
|
|
<div></div>
|
|
|
|
<p>Added this... (notice no border)</p>
|
|
<script>
|
|
|
|
$("div").css("border", "2px solid red")
|
|
.add("p")
|
|
.css("background", "yellow");
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-1">
|
|
<h4>Example: <span class="desc">Adds more elements, matched by the given expression, to the set of matched elements.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p>Hello</p><span>Hello Again</span>
|
|
<script>$("p").add("span").css("background", "yellow");</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-2">
|
|
<h4>Example: <span class="desc">Adds more elements, created on the fly, to the set of matched elements.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p>Hello</p>
|
|
<script>$("p").clone().add("<span>Again</span>").appendTo(document.body);</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-3">
|
|
<h4>Example: <span class="desc">Adds one or more Elements to the set of matched elements.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p>Hello</p><span id="a">Hello Again</span>
|
|
<script>$("p").add(document.getElementById("a")).css("background", "yellow");</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-4">
|
|
<h4>Example: <span class="desc">Demonstrates how to add (or push) elements to an existing collection</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p>Hello</p><span id="a">Hello Again</span>
|
|
<script>var collection = $("p");
|
|
// capture the new collection
|
|
collection = collection.add(document.getElementById("a"));
|
|
collection.css("background", "yellow");</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body></html> |