PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/1.4.2/docs/jQuery/index.html

https://github.com/mustardamus/jquery-api-scrape
HTML | 246 lines | 239 code | 7 blank | 0 comment | 0 complexity | 8105510b4438f5115ab3b104f4127e4d MD5 | raw file
  1. <!DOCTYPE html>
  2. <html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
  3. <div class="entry-content">
  4. <div class="entry-title roundTop">
  5. <h1 class="jq-clearfix">jQuery()</h1>
  6. <div class="entry-meta">Categories:
  7. <span class="category"><a href="http://api.jquery.com/category/core/" title="View all posts in Core">Core</a></span>
  8. </div>
  9. </div>
  10. <fieldset class="toc">
  11. <legend>Contents:</legend>
  12. <ul class="toc-list">
  13. <li>
  14. <a href="#jQuery1">jQuery( selector, [ context ] ) </a><ul>
  15. <li>jQuery( selector, [ context ] )
  16. </li>
  17. <li>jQuery( element )
  18. </li>
  19. <li>jQuery( elementArray )
  20. </li>
  21. <li>jQuery( jQuery object )
  22. </li>
  23. <li>jQuery()
  24. </li>
  25. </ul>
  26. </li>
  27. <li>
  28. <a href="#jQuery2">jQuery( html, [ ownerDocument ] ) </a><ul>
  29. <li>jQuery( html, [ ownerDocument ] )
  30. </li>
  31. <li>jQuery( html, props )
  32. </li>
  33. </ul>
  34. </li>
  35. <li>
  36. <a href="#jQuery3">jQuery( callback ) </a><ul><li>jQuery( callback )
  37. </li></ul>
  38. </li>
  39. </ul>
  40. </fieldset>
  41. <div class="entry method" id="jQuery1">
  42. <h2 class="jq-clearfix roundTop section-title">
  43. <span class="name">jQuery( selector, [ context ] )</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#jQuery">jQuery</a></span>
  44. </h2>
  45. <div class="jq-box roundBottom entry-details">
  46. <p class="desc"><strong>Description: </strong>Accepts a string containing a CSS selector which is then used to match a set of elements.</p>
  47. <ul class="signatures">
  48. <li class="signature" id="jQuery-selector-context">
  49. <h4 class="name">
  50. <span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>jQuery( selector, [ context ] )</h4>
  51. <p class="arguement"><strong>selector</strong>A string containing a selector expression</p>
  52. <p class="arguement"><strong>context</strong>A DOM Element, Document, or jQuery to use as context</p>
  53. </li>
  54. <li class="signature" id="jQuery-element">
  55. <h4 class="name">
  56. <span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>jQuery( element )</h4>
  57. <p class="arguement"><strong>element</strong>A DOM element to wrap in a jQuery object.</p>
  58. </li>
  59. <li class="signature" id="jQuery-elementArray">
  60. <h4 class="name">
  61. <span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>jQuery( elementArray )</h4>
  62. <p class="arguement"><strong>elementArray</strong>An array containing a set of DOM elements to wrap in a jQuery object.</p>
  63. </li>
  64. <li class="signature" id="jQuery-jQueryobject">
  65. <h4 class="name">
  66. <span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>jQuery( jQuery object )</h4>
  67. <p class="arguement"><strong>jQuery object</strong>An existing jQuery object to clone.</p>
  68. </li>
  69. <li class="signature" id="jQuery"><h4 class="name">
  70. <span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>jQuery()</h4></li>
  71. </ul>
  72. <div class="longdesc">
  73. <p>In the first formulation listed above, <code>jQuery()</code> which can also be written as <code>$()</code> searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements:</p>
  74. <pre>$('div.foo');</pre>
  75. <h4 id="selector-context">Selector Context</h4>
  76. <p>By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the <code>$()</code> function. For example, if within a callback function we wish to do a search for an element, we can restrict that search:</p>
  77. <pre>
  78. $('div.foo').click(function() {
  79. $('span', this).addClass('bar');
  80. });
  81. </pre>
  82. <p>Since we've restricted the span selector to the context of <code>this</code>, only spans within the clicked element will get the additional class.</p>
  83. <p>Internally, selector context is implemented with the <code>.find()</code> method, so <code>$('span', this)</code> is equivalent to <code>$(this).find('span')</code>.</p>
  84. <h4 id="using-dom-elements">Using DOM elements</h4>
  85. <p>The second and third formulations of this function allow us to create a jQuery object using a DOM element or elements that we have already found in some other way. A common use of this facility is to call jQuery methods on an element that has been passed to a callback function through the keyword <code>this</code>:</p>
  86. <pre>
  87. $('div.foo').click(function() {
  88. $(this).slideUp();
  89. });
  90. </pre>
  91. <p>This example causes elements to be hidden with a sliding animation when clicked. Because the handler receives the clicked item in the <code>this</code> keyword as a bare DOM element, the element must be wrapped in a jQuery object before we can call jQuery methods on it.</p>
  92. <p>When XML data is returned from an Ajax call, we can use the <code>$()</code> function to wrap it in a jQuery object that we can easily work with. Once this is done, we can retrieve individual elements of the XML structure using <code>.find()</code> and other DOM traversal methods.</p>
  93. <h4 id="cloning-jquery-objects">Cloning jQuery Objects</h4>
  94. <p>When a jQuery object is passed as a parameter to the <code>$()</code> function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.</p>
  95. <h4 id="returning-empty-set">Returning an Empty Set</h4>
  96. <p>As of jQuery 1.4, calling the <code>jQuery()</code> method with <em>no arguments</em> returns an empty jQuery set. In previous versions of jQuery, this would return a set containing the document node.</p>
  97. </div>
  98. <h3>Examples:</h3>
  99. <div class="entry-examples" id="entry-examples">
  100. <div id="example-0">
  101. <h4>Example: <span class="desc">Finds all p elements that are children of a div element.</span>
  102. </h4>
  103. <pre><code class="example demo-code">&lt;!DOCTYPE html&gt;
  104. &lt;html&gt;
  105. &lt;head&gt;
  106. &lt;script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
  107. &lt;/head&gt;
  108. &lt;body&gt;
  109. &lt;p&gt;one&lt;/p&gt; &lt;div&gt;&lt;p&gt;two&lt;/p&gt;&lt;/div&gt; &lt;p&gt;three&lt;/p&gt;
  110. &lt;script&gt;$("div &gt; p").css("border", "1px solid gray");&lt;/script&gt;
  111. &lt;/body&gt;
  112. &lt;/html&gt;</code></pre>
  113. <h4>Demo:</h4>
  114. <div class="demo code-demo"></div>
  115. <h4>Result:</h4>
  116. <pre><code class="results">[ &lt;p&gt;two&lt;/p&gt; ] </code></pre>
  117. </div>
  118. <div id="example-1">
  119. <h4>Example: <span class="desc">Finds all inputs of type radio within the first form in the document.</span>
  120. </h4>
  121. <pre><code class="example">$("input:radio", document.forms[0]);</code></pre>
  122. </div>
  123. <div id="example-2">
  124. <h4>Example: <span class="desc">Finds all div elements within an XML document from an Ajax response.</span>
  125. </h4>
  126. <pre><code class="example">$("div", xml.responseXML);</code></pre>
  127. </div>
  128. <div id="example-3">
  129. <h4>Example: <span class="desc">Sets the background color of the page to black.</span>
  130. </h4>
  131. <pre><code class="example">$(document.body).css( "background", "black" );</code></pre>
  132. </div>
  133. <div id="example-4">
  134. <h4>Example: <span class="desc">Hides all the input elements within a form.</span>
  135. </h4>
  136. <pre><code class="example">$(myForm.elements).hide()</code></pre>
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. <div class="entry method" id="jQuery2">
  142. <h2 class="jq-clearfix roundTop section-title">
  143. <span class="name">jQuery( html, [ ownerDocument ] )</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#jQuery">jQuery</a></span>
  144. </h2>
  145. <div class="jq-box roundBottom entry-details">
  146. <p class="desc"><strong>Description: </strong>Creates DOM elements on the fly from the provided string of raw HTML.</p>
  147. <ul class="signatures">
  148. <li class="signature" id="jQuery-html-ownerDocument">
  149. <h4 class="name">
  150. <span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>jQuery( html, [ ownerDocument ] )</h4>
  151. <p class="arguement"><strong>html</strong>A string of HTML to create on the fly. Note that this parses HTML, <strong>not</strong> XML.</p>
  152. <p class="arguement"><strong>ownerDocument</strong>A document in which the new elements will be created</p>
  153. </li>
  154. <li class="signature" id="jQuery-html-props">
  155. <h4 class="name">
  156. <span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>jQuery( html, props )</h4>
  157. <p class="arguement"><strong>html</strong>A string defining a single, standalone, HTML element (e.g. &lt;div/&gt; or &lt;div&gt;&lt;/div&gt;).</p>
  158. <p class="arguement"><strong>props</strong>An map of attributes, events, and methods to call on the newly-created element.</p>
  159. </li>
  160. </ul>
  161. <div class="longdesc">
  162. <h4 id="creating-new-elements">Creating New Elements</h4>
  163. <p>If a string is passed as the parameter to <code>$()</code>, jQuery examines the string to see if it looks like HTML (i.e., it has <code>&lt;tag ... &gt;</code> somewhere within the string). If not, the string is interpreted as a selector expression, as explained above. But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML. Then a jQuery object is created and returned that refers to these elements. We can perform any of the usual jQuery methods on this object:</p>
  164. <pre>$('&lt;p id="test"&gt;My &lt;em&gt;new&lt;/em&gt; text&lt;/p&gt;').appendTo('body');</pre>
  165. <p>When the HTML is more complex than a single tag without attributes, as it is in the above example, the actual creation of the elements is handled by the browser's <code>innerHTML</code> mechanism. Specifically, jQuery creates a new &lt;div&gt; element and sets the innerHTML property of the element to the HTML snippet that was passed in. When the parameter has a single tag, such as <code>$('&lt;img /&gt;')</code> or <code>$('&lt;a&gt;&lt;/a&gt;')</code>, jQuery creates the element using the native JavaScript <code>createElement()</code> function.</p>
  166. <p>To ensure cross-platform compatibility, the snippet must be well-formed. Tags that can contain other elements should be paired with a closing tag:</p>
  167. <pre>$('&lt;a href="http://jquery.com"&gt;&lt;/a&gt;');</pre>
  168. <p>Alternatively, jQuery allows XML-like tag syntax (with or without a space before the slash):</p>
  169. <pre>$('&lt;a/&gt;');</pre>
  170. <p>Tags that cannot contain elements may be quick-closed or not:</p>
  171. <pre>$('&lt;img /&gt;');
  172. $('&lt;input&gt;');
  173. </pre>
  174. <p>As of jQuery 1.4, we can pass a map of properties to the second argument. This argument accepts a superset of properties that can be passed to the <a href="/attr">.attr()</a> method. Furthermore, any <a href="/category/events/">event type</a> can be passed in, and the following jQuery methods can be called: <a href="/val">val</a>, <a href="/css">css</a>, <a href="/html">html</a>, <a href="/text">text</a>, <a href="/data">data</a>, <a href="/width">width</a>, <a href="/height">height</a>, or <a href="/offset">offset</a>. Note that Internet Explorer will not allow you to create an <code>input</code> element and change its type; you must specify the type using <code>'&lt;input type="checkbox" /&gt;'</code> for example.</p>
  175. </div>
  176. <h3>Examples:</h3>
  177. <div class="entry-examples" id="entry-examples-1">
  178. <div id="example-1-0">
  179. <h4>Example: <span class="desc">Creates a div element (and all of its contents) dynamically, and appends it to the body element. Internally, an element is created and its innerHTML property set to the given markup. It is therefore both quite flexible and limited.</span>
  180. </h4>
  181. <pre><code class="example">$("&lt;div&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;/div&gt;").appendTo("body")</code></pre>
  182. </div>
  183. <div id="example-1-1">
  184. <h4>Example: <span class="desc">Create some DOM elements.</span>
  185. </h4>
  186. <pre><code class="example">$("&lt;div/&gt;", {
  187. "class": "test",
  188. text: "Click me!",
  189. click: function(){
  190. $(this).toggleClass("test");
  191. }
  192. }).appendTo("body");
  193. $("&lt;input&gt;", {
  194. type: "text",
  195. val: "Test",
  196. focusin: function() {
  197. $(this).addClass("active");
  198. },
  199. focusout: function() {
  200. $(this).removeClass("active");
  201. }
  202. }).appendTo("form");</code></pre>
  203. </div>
  204. </div>
  205. </div>
  206. </div>
  207. <div class="entry method" id="jQuery3">
  208. <h2 class="jq-clearfix roundTop section-title">
  209. <span class="name">jQuery( callback )</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#jQuery">jQuery</a></span>
  210. </h2>
  211. <div class="jq-box roundBottom entry-details">
  212. <p class="desc"><strong>Description: </strong>Binds a function to be executed when the DOM has finished loading.</p>
  213. <ul class="signatures"><li class="signature" id="jQuery-callback">
  214. <h4 class="name">
  215. <span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>jQuery( callback )</h4>
  216. <p class="arguement"><strong>callback</strong>The function to execute when the DOM is ready.</p>
  217. </li></ul>
  218. <div class="longdesc"><p>This function behaves just like <code>$(document).ready()</code>, in that it should be used to wrap other <code>$()</code> operations on your page that depend on the DOM being ready. While this function is, technically, chainable, there really isn't much use for chaining against it.</p></div>
  219. <h3>Examples:</h3>
  220. <div class="entry-examples" id="entry-examples-2">
  221. <div id="example-2-0">
  222. <h4>Example: <span class="desc">Executes the function when the DOM is ready to be used.</span>
  223. </h4>
  224. <pre><code class="example">$(function(){
  225. // Document is ready
  226. });</code></pre>
  227. </div>
  228. <div id="example-2-1">
  229. <h4>Example: <span class="desc">Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias.</span>
  230. </h4>
  231. <pre><code class="example">jQuery(function($) {
  232. // Your code using failsafe $ alias here...
  233. });</code></pre>
  234. </div>
  235. </div>
  236. </div>
  237. </div>
  238. </div>
  239. </body></html>