Skip to content

Commit aa8ac67

Browse files
committed
jQuery(): Clean up content and code style.
1 parent e71eabb commit aa8ac67

File tree

1 file changed

+56
-48
lines changed

1 file changed

+56
-48
lines changed

entries/jQuery.xml

+56-48
Original file line numberDiff line numberDiff line change
@@ -42,73 +42,76 @@
4242
<desc>Accepts a string containing a CSS selector which is then used to match a set of elements.</desc>
4343
<longdesc>
4444
<p>In the first formulation listed above, <code>jQuery()</code> &#x2014; which can also be written as <code>$()</code> &#x2014; searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements:</p>
45-
<pre><code>$('div.foo');</code></pre>
45+
<pre><code>$( "div.foo" );</code></pre>
4646
<p>If no elements match the provided selector, the new jQuery object is "empty"; that is, it contains no elements and has <code><a href="http://api.jquery.com/length/">.length</a></code> property of 0.</p>
4747
<h4 id="selector-context">Selector Context</h4>
48-
<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, to do a search within an event handler, the search can be restricted like so:</p>
48+
<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, to do a search within an event handler, the search can be restricted like so:</p>
4949
<pre><code>
50-
$('div.foo').click(function() {
51-
$('span', this).addClass('bar');
50+
$( "div.foo" ).click(function() {
51+
$( "span", this ).addClass( "bar" );
5252
});
5353
</code></pre>
5454
<p>When the search for the span selector is restricted to the context of <code>this</code>, only spans within the clicked element will get the additional class.</p>
55-
<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>
55+
<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>
56+
5657
<h4 id="using-dom-elements">Using DOM elements</h4>
5758
<p>The second and third formulations of this function create a jQuery object using one or more DOM elements that were already selected in some other way.</p>
58-
<p><strong>Note:</strong>These formulations are meant to consume only DOM elements; feeding mixed data to the elementArray form is particularly discouraged.</p>
59+
<p><strong>Note:</strong> These formulations are meant to consume only DOM elements; feeding mixed data to the elementArray form is particularly discouraged.</p>
5960
<p>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>
6061
<pre><code>
61-
$('div.foo').click(function() {
62+
$( "div.foo" ).click(function() {
6263
$(this).slideUp();
6364
});
6465
</code></pre>
6566
<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 passed to the <code>$()</code> function before applying jQuery methods to it.</p>
6667
<p>XML data returned from an Ajax call can be passed to the <code>$()</code> function so individual elements of the XML structure can be retrieved using <code>.find()</code> and other DOM traversal methods.</p>
6768
<pre><code>
68-
$.post('url.xml', function(data) {
69-
var $child = $(data).find('child');
69+
$.post( "url.xml", function(data) {
70+
var $child = $(data).find("child");
7071
})
7172
</code></pre>
73+
7274
<h4 id="cloning-jquery-objects">Cloning jQuery Objects</h4>
7375
<p>When a jQuery object is passed 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>
76+
7477
<h4 id="returning-empty-set">Returning an Empty Set</h4>
7578
<p>As of jQuery 1.4, calling the <code>jQuery()</code> method with <em>no arguments</em> returns an empty jQuery set (with a <code><a href="http://api.jquery.com/length/">.length</a></code> property of 0). In previous versions of jQuery, this would return a set containing the document node.</p>
7679
<h4 id="working-with-plain-objects">Working With Plain Objects</h4>
7780
<p>At present, the only operations supported on plain JavaScript objects wrapped in jQuery are: <code>.data()</code>,<code>.prop()</code>,<code>.bind()</code>, <code>.unbind()</code>, <code>.trigger()</code> and <code>.triggerHandler()</code>. The use of <code>.data()</code> (or any method requiring <code>.data()</code>) on a plain object will result in a new property on the object called jQuery{randomNumber} (eg. jQuery123456789).</p>
7881
<pre><code>
7982
// define a plain object
80-
var foo = {foo:'bar', hello:'world'};
83+
var foo = {foo: "bar", hello: "world"};
8184

82-
// wrap this with jQuery
83-
var $foo = $(foo);
85+
// Pass it to the jQuery function
86+
var $foo = $( foo );
8487

8588
// test accessing property values
86-
var test1 = $foo.prop('foo'); // bar
89+
var test1 = $foo.prop( "foo" ); // bar
8790

8891
// test setting property values
89-
$foo.prop('foo', 'foobar');
90-
var test2 = $foo.prop('foo'); // foobar
92+
$foo.prop( "foo", "foobar" );
93+
var test2 = $foo.prop( "foo" ); // foobar
9194

9295
// test using .data() as summarized above
93-
$foo.data('keyName', 'someValue');
94-
console.log($foo); // will now contain a jQuery{randomNumber} property
96+
$foo.data( "keyName", "someValue");
97+
console.log( $foo ); // will now contain a jQuery{randomNumber} property
9598

9699
// test binding an event name and triggering
97-
$foo.bind('eventName', function (){
98-
console.log('eventName was called');
100+
$foo.bind( "eventName", function () {
101+
console.log("eventName was called");
99102
});
100103

101-
$foo.trigger('eventName'); // logs 'eventName was called'
104+
$foo.trigger( "eventName" ); // logs "eventName was called"
102105
</code></pre>
103-
<p>Should <code>.trigger('eventName')</code> be used, it will search for an 'eventName' property on the object and attempt to execute it after any attached jQuery handlers are executed. It does not check whether the property is a function or not. To avoid this behavior, <code>.triggerHandler('eventName')</code> should be used instead.</p>
106+
<p>Should <code>.trigger( "eventName" )</code> be used, it will search for an "eventName" property on the object and attempt to execute it after any attached jQuery handlers are executed. It does not check whether the property is a function or not. To avoid this behavior, <code>.triggerHandler( "eventName" )</code> should be used instead.</p>
104107
<pre><code>
105-
$foo.triggerHandler('eventName'); // also logs 'eventName was called'
108+
$foo.triggerHandler( "eventName" ); // also logs "eventName was called"
106109
</code></pre>
107110
</longdesc>
108111
<example>
109112
<desc>Find all p elements that are children of a div element and apply a border to them.</desc>
110113
<code><![CDATA[
111-
$("div > p").css("border", "1px solid gray");
114+
$( "div > p").css("border", "1px solid gray");
112115
]]></code>
113116
<html><![CDATA[<p>one</p> <div><p>two</p></div> <p>three</p>]]></html>
114117
</example>
@@ -155,58 +158,63 @@ $foo.triggerHandler('eventName'); // also logs 'eventName was called'
155158
<longdesc>
156159
<h4 id="creating-new-elements">Creating New Elements</h4>
157160
<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. You can perform any of the usual jQuery methods on this object:</p>
158-
<pre>$('&lt;p id="test"&gt;My &lt;em&gt;new&lt;/em&gt; text&lt;/p&gt;').appendTo('body');</pre>
159-
<p>If 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. In most cases, 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 (with optional closing tag or quick-closing) — <code>$('&lt;img&#xA0;/&gt;')</code> or <code>$('&lt;img&gt;')</code>, <code>$('&lt;a&gt;&lt;/a&gt;')</code> or <code>$('&lt;a&gt;')</code> — jQuery creates the element using the native JavaScript <code>createElement()</code> function.</p>
160-
<p>When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, jQuery uses the browser's <code>.innerHTML</code> property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as <code>&lt;html&gt;</code>, <code>&lt;title&gt;</code>, or <code>&lt;head&gt;</code> elements. As a result, the elements inserted may not be representative of the original string passed.</p>
161-
<p> Filtering isn't, however, limited to these tags. For example, Internet Explorer prior to version 8 will also convert all <code>href</code> properties on links to absolute URLs, and Internet Explorer prior to version 9 will not correctly handle HTML5 elements without the addition of a separate <a href="http://code.google.com/p/html5shiv/">compatibility layer</a>.</p>
161+
<pre><code>$( "&lt;p id='test'&gt;My &lt;em&gt;new&lt;/em&gt; text&lt;/p&gt;" ).appendTo( "body" );</code></pre>
162+
<p>If 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. In most cases, 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 (with optional closing tag or quick-closing) — <code>$( "&lt;img&#xA0;/&gt;" )</code> or <code>$( "&lt;img&gt;" )</code>, <code>$( "&lt;a&gt;&lt;/a&gt;" )</code> or <code>$( "&lt;a&gt;" )</code> — jQuery creates the element using the native JavaScript <code>createElement()</code> function.</p>
163+
<p>When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, jQuery uses the browser"s <code>.innerHTML</code> property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as <code>&lt;html&gt;</code>, <code>&lt;title&gt;</code>, or <code>&lt;head&gt;</code> elements. As a result, the elements inserted may not be representative of the original string passed.</p>
164+
<p>Filtering isn't, however, limited to these tags. For example, Internet Explorer prior to version 8 will also convert all <code>href</code> properties on links to absolute URLs, and Internet Explorer prior to version 9 will not correctly handle HTML5 elements without the addition of a separate <a href="http://code.google.com/p/html5shiv/">compatibility layer</a>.</p>
162165
<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>
163-
<pre><code>$('&lt;a href="http://jquery.com"&gt;&lt;/a&gt;');</code></pre>
166+
<pre><code>$( "&lt;a href='http://jquery.com'&gt;&lt;/a&gt;" );</code></pre>
164167
<p>Tags that cannot contain elements may be quick-closed or not:</p>
165-
<pre><code>$('&lt;img /&gt;');
166-
$('&lt;input&gt;');
168+
<pre><code>$( "&lt;img /&gt;" );
169+
$( "&lt;input&gt;" );
167170
</code></pre>
168171
<p>When passing HTML to <code>jQuery()</code>, please also note that text nodes are not treated as DOM elements. With the exception of a few methods (such as <code>.content()</code>), they are generally otherwise ignored or removed. E.g:</p>
169172
<pre><code>
170-
var el = $('1&lt;br/&gt;2&lt;br/&gt;3'); // returns [&lt;br&gt;, "2", &lt;br&gt;]
171-
el = $('1&lt;br/&gt;2&lt;br/&gt;3 &gt;'); // returns [&lt;br&gt;, "2", &lt;br&gt;, "3 &amp;gt;"]
173+
var el = $( "1&lt;br/&gt;2&lt;br/&gt;3" ); // returns [&lt;br&gt;, "2", &lt;br&gt;]
174+
el = $( "1&lt;br/&gt;2&lt;br/&gt;3 &gt;" ); // returns [&lt;br&gt;, "2", &lt;br&gt;, "3 &amp;gt;"]
172175
</code></pre>
173-
<p>This behaviour is expected. </p>
176+
<p>This behavior is expected. </p>
174177
<p>As of jQuery 1.4, the second argument to <code>jQuery()</code> can accept a plain object consisting of a superset of the 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>.</p>
175178
<p><strong>As of jQuery 1.8</strong>, any jQuery instance method (a method of <code>jQuery.fn</code>) can be used as a property of the object passed to the second parameter:</p>
176179
<pre><code>
177180
$( "<div></div>", {
178-
append: "<span>hello</span>"
181+
"class": "my-div",
182+
on: {
183+
touchstart: function( event ) {
184+
// do something
185+
}
186+
}
179187
}).appendTo( "body" );
180188
</code></pre>
181189
<p>The name <code>"class"</code> must be quoted in the map since it is a JavaScript reserved word, and <code>"className"</code> cannot be used since it is not the correct attribute name. </p>
182-
<p><strong>Note:</strong> Internet Explorer will not allow you to create an <code>input</code> or <code>button</code> element and change its type; you must specify the type using <code>'&lt;input type="checkbox" /&gt;'</code> for example. A demonstration of this can be seen below:</p>
190+
<p><strong>Note:</strong> Internet Explorer will not allow you to create an <code>input</code> or <code>button</code> element and change its type; you must specify the type using <code>"&lt;input type="checkbox" /&gt;"</code>, for example. A demonstration of this can be seen below:</p>
183191
<p>Unsupported in IE:</p>
184192
<pre><code>
185-
$('&lt;input /&gt;', {
186-
type: 'text',
187-
name: 'test'
188-
}).appendTo("body");
193+
$( "&lt;input /&gt;", {
194+
type: "text",
195+
name: "test"
196+
}).appendTo( "body" );
189197
</code></pre>
190198
<p>Supported workaround:</p>
191199
<pre><code>
192-
$('&lt;input type="text" /&gt;').attr({
193-
name: 'test'
194-
}).appendTo("body");
200+
$( "&lt;input type="text" /&gt;" ).attr({
201+
name: "test"
202+
}).appendTo( "body" );
195203
</code></pre>
196204
</longdesc>
197205
<example>
198206
<desc>Create a div element (and all of its contents) dynamically and append it to the body element. Internally, an element is created and its innerHTML property set to the given markup.</desc>
199-
<code><![CDATA[$("<div><p>Hello</p></div>").appendTo("body")]]></code>
207+
<code><![CDATA[$( "<div><p>Hello</p></div>" ).appendTo( "body" )]]></code>
200208
</example>
201209
<example>
202210
<desc>Create some DOM elements.</desc>
203-
<code><![CDATA[$("<div/>", {
211+
<code><![CDATA[$( "<div/>", {
204212
"class": "test",
205213
text: "Click me!",
206-
click: function(){
207-
$(this).toggleClass("test");
214+
click: function() {
215+
$( this ).toggleClass( "test" );
208216
}
209-
}).appendTo("body");]]></code>
217+
}).appendTo( "body" );]]></code>
210218
</example>
211219
<category slug="core"/>
212220
<category slug="version/1.0"/>
@@ -221,7 +229,7 @@ $('&lt;input type="text" /&gt;').attr({
221229
</signature>
222230
<desc>Binds a function to be executed when the DOM has finished loading.</desc>
223231
<longdesc>
224-
<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>
232+
<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>
225233
</longdesc>
226234
<example>
227235
<desc>Execute the function when the DOM is ready to be used.</desc>

0 commit comments

Comments
 (0)