2 * jQuery - New Wave Javascript
4 * Copyright (c) 2006 John Resig (jquery.com)
5 * Licensed under the MIT License:
6 * http://www.opensource.org/licenses/mit-license.php
12 // Global undefined variable
13 window.undefined = window.undefined;
16 * Create a new jQuery Object
19 function jQuery(a,c) {
21 // Shortcut for document ready (because $(document).each() is silly)
22 if ( a && a.constructor == Function && jQuery.fn.ready )
23 return jQuery(document).ready(a);
25 // Make sure t hat a selection was provided
26 a = a || jQuery.context || document;
29 * Handle support for overriding other $() functions. Way too many libraries
30 * provide this function to simply ignore it and overwrite it.
33 // Check to see if this is a possible collision case
34 if ( jQuery._$ && !c && a.constructor == String &&
36 // Make sure that the expression is a colliding one
37 !/[^a-zA-Z0-9_-]/.test(a) &&
39 // and that there are no elements that match it
40 // (this is the one truly ambiguous case)
41 !document.getElementsByTagName(a).length )
43 // Use the default method, in case it works some voodoo
44 return jQuery._$( a );
47 // Watch for when a jQuery object is passed as the selector
51 // Watch for when a jQuery object is passed at the context
53 return jQuery(c.get()).find(a);
55 // If the context is global, return a new object
57 return new jQuery(a,c);
59 // Handle HTML strings
60 var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
61 if ( m ) a = jQuery.clean( [ m[1] ] );
63 // Watch for when an array is passed in
64 this.get( a.constructor == Array || a.length && !a.nodeType && a[0] != undefined && a[0].nodeType ?
65 // Assume that it is an array of DOM Elements
66 jQuery.merge( a, [] ) :
68 // Find the matching elements and save them for later
69 jQuery.find( a, c ) );
71 var fn = arguments[ arguments.length - 1 ];
72 if ( fn && fn.constructor == Function )
76 // Map over the $ in case of overwrite
80 // Map the jQuery namespace to the '$' one
83 jQuery.fn = jQuery.prototype = {
85 * The current SVN version of jQuery.
95 * The number of elements currently matched.
97 * @example $("img").length;
98 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
107 * The number of elements currently matched.
109 * @example $("img").size();
110 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
121 * Access all matched elements. This serves as a backwards-compatible
122 * way of accessing all matched elements (other than the jQuery object
123 * itself, which is, in fact, an array of elements).
125 * @example $("img").get();
126 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
127 * @result [ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
130 * @type Array<Element>
134 * Access a single matched element. <tt>num</tt> is used to access the
135 * <tt>num</tt>th element matched.
137 * @example $("img").get(1);
138 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
139 * @result [ <img src="test1.jpg"/> ]
143 * @param Number num Access the element in the <tt>num</tt>th position.
147 * Set the jQuery object to an array of elements.
149 * @example $("img").get([ document.body ]);
150 * @result $("img").get() == [ document.body ]
155 * @param Elements elems An array of elements
157 get: function( num ) {
158 // Watch for when an array (of elements) is passed in
159 if ( num && num.constructor == Array ) {
161 // Use a tricky hack to make the jQuery object
162 // look and feel like an array
164 [].push.apply( this, num );
168 return num == undefined ?
170 // Return a 'clean' array
171 jQuery.map( this, function(a){ return a } ) :
173 // Return just the object
178 * Execute a function within the context of every matched element.
179 * This means that every time the passed-in function is executed
180 * (which is once for every element matched) the 'this' keyword
181 * points to the specific element.
183 * Additionally, the function, when executed, is passed a single
184 * argument representing the position of the element in the matched
187 * @example $("img").each(function(){ this.src = "test.jpg"; });
188 * @before <img/> <img/>
189 * @result <img src="test.jpg"/> <img src="test.jpg"/>
193 * @param Function fn A function to execute
195 each: function( fn, args ) {
196 // Iterate through all of the matched elements
197 for ( var i = 0; i < this.length; i++ )
199 // Execute the function within the context of each element
200 fn.apply( this[i], args || [i] );
206 * Access a property on the first matched element.
207 * This method makes it easy to retreive a property value
208 * from the first matched element.
210 * @example $("img").attr("src");
211 * @before <img src="test.jpg"/>
216 * @param String name The name of the property to access.
220 * Set a hash of key/value object properties to all matched elements.
221 * This serves as the best way to set a large number of properties
222 * on all matched elements.
224 * @example $("img").attr({ src: "test.jpg", alt: "Test Image" });
226 * @result <img src="test.jpg" alt="Test Image"/>
230 * @param Hash prop A set of key/value pairs to set as object properties.
234 * Set a single property to a value, on all matched elements.
236 * @example $("img").attr("src","test.jpg");
238 * @result <img src="test.jpg"/>
242 * @param String key The name of the property to set.
243 * @param Object value The value to set the property to.
245 attr: function( key, value, type ) {
246 // Check to see if we're setting style values
247 return key.constructor != String || value ?
248 this.each(function(){
249 // See if we're setting a hash of styles
250 if ( value == undefined )
251 // Set all the styles
252 for ( var prop in key )
254 type ? this.style : this,
258 // See if we're setting a single key/value style
261 type ? this.style : this,
266 // Look for the case where we're accessing a style value
267 jQuery[ type || "attr" ]( this[0], key );
271 * Access a style property on the first matched element.
272 * This method makes it easy to retreive a style property value
273 * from the first matched element.
275 * @example $("p").css("red");
276 * @before <p style="color:red;">Test Paragraph.</p>
281 * @param String name The name of the property to access.
285 * Set a hash of key/value style properties to all matched elements.
286 * This serves as the best way to set a large number of style properties
287 * on all matched elements.
289 * @example $("p").css({ color: "red", background: "blue" });
290 * @before <p>Test Paragraph.</p>
291 * @result <p style="color:red; background:blue;">Test Paragraph.</p>
295 * @param Hash prop A set of key/value pairs to set as style properties.
299 * Set a single style property to a value, on all matched elements.
301 * @example $("p").css("color","red");
302 * @before <p>Test Paragraph.</p>
303 * @result <p style="color:red;">Test Paragraph.</p>
307 * @param String key The name of the property to set.
308 * @param Object value The value to set the property to.
310 css: function( key, value ) {
311 return this.attr( key, value, "curCSS" );
315 * Retreive the text contents of all matched elements. The result is
316 * a string that contains the combined text contents of all matched
317 * elements. This method works on both HTML and XML documents.
319 * @example $("p").text();
320 * @before <p>Test Paragraph.</p>
321 * @result Test Paragraph.
329 for ( var j = 0; j < e.length; j++ ) {
330 var r = e[j].childNodes;
331 for ( var i = 0; i < r.length; i++ )
332 t += r[i].nodeType != 1 ?
333 r[i].nodeValue : jQuery.fn.text([ r[i] ]);
339 * Wrap all matched elements with a structure of other elements.
340 * This wrapping process is most useful for injecting additional
341 * stucture into a document, without ruining the original semantic
342 * qualities of a document.
344 * The way that is works is that it goes through the first element argument
345 * provided and finds the deepest element within the structure - it is that
346 * element that will en-wrap everything else.
348 * @example $("p").wrap("<div class='wrap'></div>");
349 * @before <p>Test Paragraph.</p>
350 * @result <div class='wrap'><p>Test Paragraph.</p></div>
354 * @any String html A string of HTML, that will be created on the fly and wrapped around the target.
355 * @any Element elem A DOM element that will be wrapped.
356 * @any Array<Element> elems An array of elements, the first of which will be wrapped.
357 * @any Object obj Any object, converted to a string, then a text node.
360 // The elements to wrap the target around
361 var a = jQuery.clean(arguments);
363 // Wrap each of the matched elements individually
364 return this.each(function(){
365 // Clone the structure that we're using to wrap
366 var b = a[0].cloneNode(true);
368 // Insert it before the element to be wrapped
369 this.parentNode.insertBefore( b, this );
371 // Find he deepest point in the wrap structure
372 while ( b.firstChild )
375 // Move the matched element to within the wrap structure
376 b.appendChild( this );
381 * Append any number of elements to the inside of all matched elements.
382 * This operation is similar to doing an <tt>appendChild</tt> to all the
383 * specified elements, adding them into the document.
385 * @example $("p").append("<b>Hello</b>");
386 * @before <p>I would like to say: </p>
387 * @result <p>I would like to say: <b>Hello</b></p>
391 * @any String html A string of HTML, that will be created on the fly and appended to the target.
392 * @any Element elem A DOM element that will be appended.
393 * @any Array<Element> elems An array of elements, all of which will be appended.
394 * @any Object obj Any object, converted to a string, then a text node.
397 return this.domManip(arguments, true, 1, function(a){
398 this.appendChild( a );
403 * Prepend any number of elements to the inside of all matched elements.
404 * This operation is the best way to insert a set of elements inside, at the
405 * beginning, of all the matched element.
407 * @example $("p").prepend("<b>Hello</b>");
408 * @before <p>, how are you?</p>
409 * @result <p><b>Hello</b>, how are you?</p>
413 * @any String html A string of HTML, that will be created on the fly and prepended to the target.
414 * @any Element elem A DOM element that will be prepended.
415 * @any Array<Element> elems An array of elements, all of which will be prepended.
416 * @any Object obj Any object, converted to a string, then a text node.
418 prepend: function() {
419 return this.domManip(arguments, true, -1, function(a){
420 this.insertBefore( a, this.firstChild );
425 * Insert any number of elements before each of the matched elements.
427 * @example $("p").before("<b>Hello</b>");
428 * @before <p>how are you?</p>
429 * @result <b>Hello</b><p>how are you?</p>
433 * @any String html A string of HTML, that will be created on the fly and inserted.
434 * @any Element elem A DOM element that will beinserted.
435 * @any Array<Element> elems An array of elements, all of which will be inserted.
436 * @any Object obj Any object, converted to a string, then a text node.
439 return this.domManip(arguments, false, 1, function(a){
440 this.parentNode.insertBefore( a, this );
445 * Insert any number of elements after each of the matched elements.
447 * @example $("p").after("<p>I'm doing fine.</p>");
448 * @before <p>How are you?</p>
449 * @result <p>How are you?</p><p>I'm doing fine.</p>
453 * @any String html A string of HTML, that will be created on the fly and inserted.
454 * @any Element elem A DOM element that will beinserted.
455 * @any Array<Element> elems An array of elements, all of which will be inserted.
456 * @any Object obj Any object, converted to a string, then a text node.
459 return this.domManip(arguments, false, -1, function(a){
460 this.parentNode.insertBefore( a, this.nextSibling );
465 * End the most recent 'destructive' operation, reverting the list of matched elements
466 * back to its previous state. After an end operation, the list of matched elements will
467 * revert to the last state of matched elements.
469 * @example $("p").find("span").end();
470 * @before <p><span>Hello</span>, how are you?</p>
471 * @result $("p").find("span").end() == [ <p>...</p> ]
477 return this.get( this.stack.pop() );
481 * Searches for all elements that match the specified expression.
482 * This method is the optimal way of finding additional descendant
483 * elements with which to process.
485 * All searching is done using a jQuery expression. The expression can be
486 * written using CSS 1-3 Selector syntax, or basic XPath.
488 * @example $("p").find("span");
489 * @before <p><span>Hello</span>, how are you?</p>
490 * @result $("p").find("span") == [ <span>Hello</span> ]
494 * @param String expr An expression to search with.
497 return this.pushStack( jQuery.map( this, function(a){
498 return jQuery.find(t,a);
503 * Removes all elements from the set of matched elements that do not
504 * match the specified expression. This method is used to narrow down
505 * the results of a search.
507 * All searching is done using a jQuery expression. The expression
508 * can be written using CSS 1-3 Selector syntax, or basic XPath.
510 * @example $("p").filter(".selected")
511 * @before <p class="selected">Hello</p><p>How are you?</p>
512 * @result $("p").filter(".selected") == [ <p class="selected">Hello</p> ]
516 * @param String expr An expression to search with.
520 * Removes all elements from the set of matched elements that do not
521 * match at least one of the expressions passed to the function. This
522 * method is used when you want to filter the set of matched elements
523 * through more than one expression.
525 * Elements will be retained in the jQuery object if they match at
526 * least one of the expressions passed.
528 * @example $("p").filter([".selected", ":first"])
529 * @before <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
530 * @result $("p").filter([".selected", ":first"]) == [ <p>Hello</p>, <p class="selected">And Again</p> ]
534 * @param Array<String> exprs A set of expressions to evaluate against
536 filter: function(t) {
537 return this.pushStack(
538 t.constructor == Array &&
539 jQuery.map(this,function(a){
540 for ( var i = 0; i < t.length; i++ )
541 if ( jQuery.filter(t[i],[a]).r.length )
545 t.constructor == Boolean &&
546 ( t ? this.get() : [] ) ||
548 t.constructor == Function &&
549 jQuery.grep( this, t ) ||
551 jQuery.filter(t,this).r, arguments );
555 * Removes the specified Element from the set of matched elements. This
556 * method is used to remove a single Element from a jQuery object.
558 * @example $("p").not( document.getElementById("selected") )
559 * @before <p>Hello</p><p id="selected">Hello Again</p>
560 * @result [ <p>Hello</p> ]
564 * @param Element el An element to remove from the set
568 * Removes elements matching the specified expression from the set
569 * of matched elements. This method is used to remove one or more
570 * elements from a jQuery object.
572 * @example $("p").not("#selected")
573 * @before <p>Hello</p><p id="selected">Hello Again</p>
574 * @result [ <p>Hello</p> ]
578 * @param String expr An expression with which to remove matching elements
581 return this.pushStack( t.constructor == String ?
582 jQuery.filter(t,this,false).r :
583 jQuery.grep(this,function(a){ return a != t; }), arguments );
587 * Adds the elements matched by the expression to the jQuery object. This
588 * can be used to concatenate the result sets of two expressions.
590 * @example $("p").add("span")
591 * @before <p>Hello</p><p><span>Hello Again</span></p>
592 * @result [ <p>Hello</p>, <span>Hello Again</span> ]
596 * @param String expr An expression whose matched elements are added
600 * Adds each of the Elements in the array to the set of matched elements.
601 * This is used to add a set of Elements to a jQuery object.
603 * @example $("p").add([document.getElementById("a"), document.getElementById("b")])
604 * @before <p>Hello</p><p><span id="a">Hello Again</span><span id="b">And Again</span></p>
605 * @result [ <p>Hello</p>, <span id="a">Hello Again</span>, <span id="b">And Again</span> ]
609 * @param Array<Element> els An array of Elements to add
613 * Adds a single Element to the set of matched elements. This is used to
614 * add a single Element to a jQuery object.
616 * @example $("p").add( document.getElementById("a") )
617 * @before <p>Hello</p><p><span id="a">Hello Again</span></p>
618 * @result [ <p>Hello</p>, <span id="a">Hello Again</span> ]
622 * @param Element el An Element to add
625 return this.pushStack( jQuery.merge( this, t.constructor == String ?
626 jQuery.find(t) : t.constructor == Array ? t : [t] ), arguments );
630 * A wrapper function for each() to be used by append and prepend.
631 * Handles cases where you're trying to modify the inner contents of
632 * a table, when you actually need to work with the tbody.
635 * @param {String} expr The expression with which to filter
639 return expr ? jQuery.filter(expr,this).r.length > 0 : this.length > 0;
648 * @param Boolean table
650 * @param Function fn The function doing the DOM manipulation.
653 domManip: function(args, table, dir, fn){
654 var clone = this.size() > 1;
655 var a = jQuery.clean(args);
657 return this.each(function(){
660 if ( table && this.nodeName == "TABLE" ) {
661 var tbody = this.getElementsByTagName("tbody");
663 if ( !tbody.length ) {
664 obj = document.createElement("tbody");
665 this.appendChild( obj );
670 for ( var i = ( dir < 0 ? a.length - 1 : 0 );
671 i != ( dir < 0 ? dir : a.length ); i += dir ) {
672 fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] );
687 pushStack: function(a,args) {
688 var fn = args && args[args.length-1];
690 if ( !fn || fn.constructor != Function ) {
691 if ( !this.stack ) this.stack = [];
692 this.stack.push( this.get() );
695 var old = this.get();
697 if ( fn.constructor == Function )
698 return this.each( fn );
705 extend: function(obj,prop) {
706 if ( !prop ) { prop = obj; obj = this; }
707 for ( var i in prop ) obj[i] = prop[i];
712 jQuery.extend = jQuery.fn.extend;
715 var b = navigator.userAgent.toLowerCase();
717 // Figure out what browser is being used
719 safari: /webkit/.test(b),
720 opera: /opera/.test(b),
721 msie: /msie/.test(b) && !/opera/.test(b),
722 mozilla: /mozilla/.test(b) && !/compatible/.test(b)
725 // Check to see if the W3C box model is being used
726 jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
730 * Get a set of elements containing the unique parents of the matched
733 * @example $("p").parent()
734 * @before <div><p>Hello</p><p>Hello</p></div>
735 * @result [ <div><p>Hello</p><p>Hello</p></div> ]
742 * Get a set of elements containing the unique parents of the matched
743 * set of elements, and filtered by an expression.
745 * @example $("p").parent(".selected")
746 * @before <div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>
747 * @result [ <div class="selected"><p>Hello Again</p></div> ]
751 * @param String expr An expression to filter the parents with
753 parent: "a.parentNode",
756 * Get a set of elements containing the unique ancestors of the matched
759 * @example $("span").ancestors()
760 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
761 * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
768 * Get a set of elements containing the unique ancestors of the matched
769 * set of elements, and filtered by an expression.
771 * @example $("span").ancestors("p")
772 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
773 * @result [ <p><span>Hello</span></p> ]
777 * @param String expr An expression to filter the ancestors with
779 ancestors: jQuery.parents,
782 * A synonym for ancestors
784 parents: jQuery.parents,
787 * Get a set of elements containing the unique next siblings of each of the
788 * matched set of elements.
790 * It only returns the very next sibling, not all next siblings.
792 * @example $("p").next()
793 * @before <p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>
794 * @result [ <p>Hello Again</p>, <div><span>And Again</span></div> ]
801 * Get a set of elements containing the unique next siblings of each of the
802 * matched set of elements, and filtered by an expression.
804 * It only returns the very next sibling, not all next siblings.
806 * @example $("p").next(".selected")
807 * @before <p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>
808 * @result [ <p class="selected">Hello Again</p> ]
812 * @param String expr An expression to filter the next Elements with
814 next: "jQuery.sibling(a).next",
817 * Get a set of elements containing the unique previous siblings of each of the
818 * matched set of elements.
820 * It only returns the immediately previous sibling, not all previous siblings.
822 * @example $("p").previous()
823 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
824 * @result [ <div><span>Hello Again</span></div> ]
831 * Get a set of elements containing the unique previous siblings of each of the
832 * matched set of elements, and filtered by an expression.
834 * It only returns the immediately previous sibling, not all previous siblings.
836 * @example $("p").previous("selected")
837 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
838 * @result [ <div><span>Hello</span></div> ]
842 * @param String expr An expression to filter the previous Elements with
844 prev: "jQuery.sibling(a).prev",
847 * Get a set of elements containing all of the unique siblings of each of the
848 * matched set of elements.
850 * @example $("div").siblings()
851 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
852 * @result [ <p>Hello</p>, <p>And Again</p> ]
859 * Get a set of elements containing all of the unique siblings of each of the
860 * matched set of elements, and filtered by an expression.
862 * @example $("div").siblings("selected")
863 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
864 * @result [ <p class="selected">Hello Again</p> ]
868 * @param String expr An expression to filter the sibling Elements with
870 siblings: jQuery.sibling
873 for ( var i in axis ) new function(){
875 jQuery.fn[ i ] = function(a) {
876 var ret = jQuery.map(this,t);
877 if ( a && a.constructor == String )
878 ret = jQuery.filter(a,ret).r;
879 return this.pushStack( ret, arguments );
883 // appendTo, prependTo, beforeTo, afterTo
885 var to = ["append","prepend","before","after"];
887 for ( var i = 0; i < to.length; i++ ) new function(){
889 jQuery.fn[ n + "To" ] = function(){
891 return this.each(function(){
892 for ( var i = 0; i < a.length; i++ )
900 * Displays each of the set of matched elements if they are hidden.
902 * @example $("p").show()
903 * @before <p style="display: none">Hello</p>
904 * @result [ <p style="display: block">Hello</p> ]
910 this.style.display = this.oldblock ? this.oldblock : "";
911 if ( jQuery.css(this,"display") == "none" )
912 this.style.display = "block";
916 * Hides each of the set of matched elements if they are shown.
918 * @example $("p").hide()
919 * @before <p>Hello</p>
920 * @result [ <p style="display: none">Hello</p> ]
926 this.oldblock = jQuery.css(this,"display");
927 if ( this.oldblock == "none" )
928 this.oldblock = "block";
929 this.style.display = "none";
933 * Toggles each of the set of matched elements. If they are shown,
934 * toggle makes them hidden. If they are hidden, toggle
937 * @example $("p").toggle()
938 * @before <p>Hello</p><p style="display: none">Hello Again</p>
939 * @result [ <p style="display: none">Hello</p>, <p style="display: block">Hello Again</p> ]
945 var d = jQuery.css(this,"display");
946 $(this)[ !d || d == "none" ? "show" : "hide" ]();
950 * Adds the specified class to each of the set of matched elements.
952 * @example ("p").addClass("selected")
953 * @before <p>Hello</p>
954 * @result [ <p class="selected">Hello</p> ]
958 * @param String class A CSS class to add to the elements
960 addClass: function(c){
961 jQuery.className.add(this,c);
965 * The opposite of addClass. Removes the specified class from the
966 * set of matched elements.
968 * @example ("p").removeClass("selected")
969 * @before <p class="selected">Hello</p>
970 * @result [ <p>Hello</p> ]
974 * @param String class A CSS class to remove from the elements
976 removeClass: function(c){
977 jQuery.className.remove(this,c);
981 * Adds the specified class if it is present. Remove it if it is
984 * @example ("p").toggleClass("selected")
985 * @before <p>Hello</p><p class="selected">Hello Again</p>
986 * @result [ <p class="selected">Hello</p>, <p>Hello Again</p> ]
990 * @param String class A CSS class with which to toggle the elements
992 toggleClass: function( c ){
993 jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this,c);
1000 if ( !a || jQuery.filter( [this], a ).r )
1001 this.parentNode.removeChild( this );
1005 * Removes all child nodes from the set of matched elements.
1007 * @example ("p").empty()
1008 * @before <p>Hello, <span>Person</span> <a href="#">and person</a></p>
1009 * @result [ <p></p> ]
1015 while ( this.firstChild )
1016 this.removeChild( this.firstChild );
1020 * Binds a particular event (like click) to a each of a set of match elements.
1022 * @example $("p").bind( "click", function() { alert("Hello"); } )
1023 * @before <p>Hello</p>
1024 * @result [ <p>Hello</p> ]
1026 * Cancel a default action and prevent it from bubbling by returning false
1027 * from your function.
1029 * @example $("form").bind( "submit", function() { return false; } )
1031 * Cancel a default action by using the preventDefault method.
1033 * @example $("form").bind( "submit", function() { e.preventDefault(); } )
1035 * Stop an event from bubbling by using the stopPropogation method.
1037 * @example $("form").bind( "submit", function() { e.stopPropogation(); } )
1041 * @param String type An event type
1042 * @param Function fn A function to bind to the event on each of the set of matched elements
1044 bind: function( type, fn ) {
1045 if ( fn.constructor == String )
1046 fn = new Function("e", ( !fn.indexOf(".") ? "$(this)" : "return " ) + fn);
1047 jQuery.event.add( this, type, fn );
1051 * The opposite of bind. Removes a bound event from each of a set of matched
1052 * elements. You must pass the identical function that was used in the original
1055 * @example $("p").unbind( "click", function() { alert("Hello"); } )
1056 * @before <p>Hello</p>
1057 * @result [ <p>Hello</p> ]
1061 * @param String type An event type
1062 * @param Function fn A function to unbind from the event on each of the set of matched elements
1064 unbind: function( type, fn ) {
1065 jQuery.event.remove( this, type, fn );
1069 * Trigger a particular event.
1071 * @example $("p").trigger("click")
1072 * @before <p>Hello</p>
1073 * @result [ <p>Hello</p> ]
1077 * @param String type An event type
1079 trigger: function( type, data ) {
1080 jQuery.event.trigger( type, data, this );
1084 for ( var i in each ) new function() {
1086 jQuery.fn[ i ] = function() {
1087 return this.each( n, arguments );
1103 for ( var i in attr ) new function() {
1104 var n = attr[i] || i;
1105 jQuery.fn[ i ] = function(h) {
1106 return h == undefined ?
1107 this.length ? this[0][n] : null :
1112 var css = "width,height,top,left,position,float,overflow,color,background".split(",");
1114 for ( var i in css ) new function() {
1116 jQuery.fn[ i ] = function(h) {
1117 return h == undefined ?
1118 ( this.length ? jQuery.css( this[0], n ) : null ) :
1128 if (jQuery.className.has(o,c)) return;
1129 o.className += ( o.className ? " " : "" ) + c;
1131 remove: function(o,c){
1132 o.className = !c ? "" :
1133 o.className.replace(
1134 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
1136 has: function(e,a) {
1139 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
1144 * Swap in/out style options.
1147 swap: function(e,o,f) {
1148 for ( var i in o ) {
1149 e.style["old"+i] = e.style[i];
1154 e.style[i] = e.style["old"+i];
1157 css: function(e,p) {
1158 if ( p == "height" || p == "width" ) {
1159 var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
1161 for ( var i in d ) {
1162 old["padding" + d[i]] = 0;
1163 old["border" + d[i] + "Width"] = 0;
1166 jQuery.swap( e, old, function() {
1167 if (jQuery.css(e,"display") != "none") {
1168 oHeight = e.offsetHeight;
1169 oWidth = e.offsetWidth;
1171 jQuery.swap( e, { visibility: "hidden", position: "absolute", display: "" },
1173 oHeight = e.clientHeight;
1174 oWidth = e.clientWidth;
1178 return p == "height" ? oHeight : oWidth;
1179 } else if ( p == "opacity" && jQuery.browser.msie )
1180 return parseFloat( jQuery.curCSS(e,"filter").replace(/[^0-9.]/,"") ) || 1;
1182 return jQuery.curCSS( e, p );
1185 curCSS: function(e,p,force) {
1188 if (!force && e.style[p])
1190 else if (e.currentStyle) {
1191 p = p.replace(/\-(\w)/g,function(m,c){return c.toUpperCase()});
1192 r = e.currentStyle[p];
1193 } else if (document.defaultView && document.defaultView.getComputedStyle) {
1194 p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
1195 var s = document.defaultView.getComputedStyle(e,"");
1196 r = s ? s.getPropertyValue(p) : null;
1202 clean: function(a) {
1204 for ( var i = 0; i < a.length; i++ ) {
1205 if ( a[i].constructor == String ) {
1207 if ( !a[i].indexOf("<tr") ) {
1209 a[i] = "<table>" + a[i] + "</table>";
1210 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
1212 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
1215 var div = document.createElement("div");
1216 div.innerHTML = a[i];
1219 div = div.firstChild.firstChild;
1220 if ( td ) div = div.firstChild;
1223 for ( var j = 0; j < div.childNodes.length; j++ )
1224 r.push( div.childNodes[j] );
1225 } else if ( a[i].jquery || a[i].length && !a[i].nodeType )
1226 for ( var k = 0; k < a[i].length; k++ )
1228 else if ( a[i] !== null )
1229 r.push( a[i].nodeType ? a[i] : document.createTextNode(a[i].toString()) );
1235 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
1236 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
1244 last: "i==r.length-1",
1249 "first-child": "jQuery.sibling(a,0).cur",
1250 "last-child": "jQuery.sibling(a,0).last",
1251 "only-child": "jQuery.sibling(a).length==1",
1254 parent: "a.childNodes.length",
1255 empty: "!a.childNodes.length",
1258 contains: "(a.innerText||a.innerHTML).indexOf(m[3])>=0",
1261 visible: "a.type!='hidden'&&jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!='hidden'",
1262 hidden: "a.type=='hidden'||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')=='hidden'",
1265 enabled: "!a.disabled",
1266 disabled: "a.disabled",
1267 checked: "a.checked"
1269 ".": "jQuery.className.has(a,m[2])",
1273 "^=": "!z.indexOf(m[4])",
1274 "$=": "z.substr(z.length - m[4].length,m[4].length)==m[4]",
1275 "*=": "z.indexOf(m[4])>=0",
1278 "[": "jQuery.find(m[2],a).length"
1282 "\\.\\.|/\\.\\.", "a.parentNode",
1283 ">|/", "jQuery.sibling(a.firstChild)",
1284 "\\+", "jQuery.sibling(a).next",
1287 var s = jQuery.sibling(a);
1289 for ( var i = s.n; i < s.length; i++ )
1295 find: function( t, context ) {
1296 // Make sure that the context is a DOM Element
1297 if ( context && context.nodeType == undefined )
1300 // Set the correct context (if none is provided)
1301 context = context || jQuery.context || document;
1303 if ( t.constructor != String ) return [t];
1305 if ( !t.indexOf("//") ) {
1306 context = context.documentElement;
1307 t = t.substr(2,t.length);
1308 } else if ( !t.indexOf("/") ) {
1309 context = context.documentElement;
1310 t = t.substr(1,t.length);
1311 // FIX Assume the root element is right :(
1312 if ( t.indexOf("/") >= 1 )
1313 t = t.substr(t.indexOf("/"),t.length);
1316 var ret = [context];
1320 while ( t.length > 0 && last != t ) {
1324 t = jQuery.trim(t).replace( /^\/\//i, "" );
1326 var foundToken = false;
1328 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
1329 var re = new RegExp("^(" + jQuery.token[i] + ")");
1333 r = ret = jQuery.map( ret, jQuery.token[i+1] );
1334 t = jQuery.trim( t.replace( re, "" ) );
1339 if ( !foundToken ) {
1340 if ( !t.indexOf(",") || !t.indexOf("|") ) {
1341 if ( ret[0] == context ) ret.shift();
1342 done = jQuery.merge( done, ret );
1343 r = ret = [context];
1344 t = " " + t.substr(1,t.length);
1346 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
1347 var m = re2.exec(t);
1349 if ( m[1] == "#" ) {
1350 // Ummm, should make this work in all XML docs
1351 var oid = document.getElementById(m[2]);
1352 r = ret = oid ? [oid] : [];
1353 t = t.replace( re2, "" );
1355 if ( !m[2] || m[1] == "." ) m[2] = "*";
1357 for ( var i = 0; i < ret.length; i++ )
1358 r = jQuery.merge( r,
1360 jQuery.getAll(ret[i]) :
1361 ret[i].getElementsByTagName(m[2])
1368 var val = jQuery.filter(t,r);
1370 t = jQuery.trim(val.t);
1374 if ( ret && ret[0] == context ) ret.shift();
1375 done = jQuery.merge( done, ret );
1380 getAll: function(o,r) {
1382 var s = o.childNodes;
1383 for ( var i = 0; i < s.length; i++ )
1384 if ( s[i].nodeType == 1 ) {
1386 jQuery.getAll( s[i], r );
1391 attr: function(o,a,v){
1392 if ( a && a.constructor == String ) {
1395 "class": "className",
1399 a = (fix[a] && fix[a].replace && fix[a] || a)
1400 .replace(/-([a-z])/ig,function(z,b){
1401 return b.toUpperCase();
1404 if ( v != undefined ) {
1406 if ( o.setAttribute && a != "disabled" )
1407 o.setAttribute(a,v);
1410 return o[a] || o.getAttribute && o.getAttribute(a) || "";
1415 // The regular expressions that power the parsing engine
1417 // Match: [@value='test'], [@foo]
1418 [ "\\[ *(@)S *([!*$^=]*) *Q\\]", 1 ],
1420 // Match: [div], [div p]
1423 // Match: :contains('foo')
1424 [ "(:)S\\(Q\\)", 0 ],
1426 // Match: :even, :last-chlid
1430 filter: function(t,r,not) {
1431 // Figure out if we're doing regular, or inverse, filtering
1432 var g = not !== false ? jQuery.grep :
1433 function(a,f) {return jQuery.grep(a,f,true);};
1435 while ( t && /^[a-z[({<*:.#]/i.test(t) ) {
1437 var p = jQuery.parse;
1439 for ( var i = 0; i < p.length; i++ ) {
1440 var re = new RegExp( "^" + p[i][0]
1442 // Look for a string-like sequence
1443 .replace( 'S', "([a-z*_-][a-z0-9_-]*)" )
1445 // Look for something (optionally) enclosed with quotes
1446 .replace( 'Q', " *'?\"?([^'\"]*)'?\"? *" ), "i" );
1448 var m = re.exec( t );
1451 // Re-organize the match
1453 m = ["", m[1], m[3], m[2], m[4]];
1455 // Remove what we just matched
1456 t = t.replace( re, "" );
1462 // :not() is a special case that can be optomized by
1463 // keeping it out of the expression list
1464 if ( m[1] == ":" && m[2] == "not" )
1465 r = jQuery.filter(m[3],r,false).r;
1467 // Otherwise, find the expression to execute
1469 var f = jQuery.expr[m[1]];
1470 if ( f.constructor != String )
1471 f = jQuery.expr[m[1]][m[2]];
1473 // Build a custom macro to enclose it
1474 eval("f = function(a,i){" +
1475 ( m[1] == "@" ? "z=jQuery.attr(a,m[3]);" : "" ) +
1476 "return " + f + "}");
1478 // Execute it against the current filter
1483 // Return an array of filtered elements (r)
1484 // and the modified expression string (t)
1485 return { r: r, t: t };
1489 * Remove the whitespace from the beginning and end of a string.
1494 * @param String str The string to trim.
1497 return t.replace(/^\s+|\s+$/g, "");
1501 * All ancestors of a given element.
1505 * @type Array<Element>
1506 * @param Element elem The element to find the ancestors of.
1508 parents: function(a){
1510 var c = a.parentNode;
1511 while ( c && c != document ) {
1519 * All elements on a specified axis.
1524 * @param Element elem The element to find all the siblings of (including itself).
1526 sibling: function(a,n) {
1528 var tmp = a.parentNode.childNodes;
1529 for ( var i = 0; i < tmp.length; i++ ) {
1530 if ( tmp[i].nodeType == 1 )
1531 type.push( tmp[i] );
1533 type.n = type.length - 1;
1535 type.last = type.n == type.length - 1;
1537 n == "even" && type.n % 2 == 0 ||
1538 n == "odd" && type.n % 2 ||
1540 type.prev = type[type.n - 1];
1541 type.next = type[type.n + 1];
1546 * Merge two arrays together, removing all duplicates.
1551 * @param Array a The first array to merge.
1552 * @param Array b The second array to merge.
1554 merge: function(a,b) {
1557 // Move b over to the new array (this helps to avoid
1558 // StaticNodeList instances)
1559 for ( var k = 0; k < b.length; k++ )
1562 // Now check for duplicates between a and b and only
1563 // add the unique items
1564 for ( var i = 0; i < a.length; i++ ) {
1567 // The collision-checking process
1568 for ( var j = 0; j < b.length; j++ )
1572 // If the item is unique, add it
1581 * Remove items that aren't matched in an array. The function passed
1582 * in to this method will be passed two arguments: 'a' (which is the
1583 * array item) and 'i' (which is the index of the item in the array).
1588 * @param Array array The Array to find items in.
1589 * @param Function fn The function to process each item against.
1590 * @param Boolean inv Invert the selection - select the opposite of the function.
1592 grep: function(a,f,s) {
1593 // If a string is passed in for the function, make a function
1594 // for it (a handy shortcut)
1595 if ( f.constructor == String )
1596 f = new Function("a","i","return " + f);
1600 // Go through the array, only saving the items
1601 // that pass the validator function
1602 for ( var i = 0; i < a.length; i++ )
1603 if ( !s && f(a[i],i) || s && !f(a[i],i) )
1610 * Translate all items in array to another array of items. The translation function
1611 * that is provided to this method is passed one argument: 'a' (the item to be
1612 * translated). If an array is returned, that array is mapped out and merged into
1613 * the full array. Additionally, returning 'null' or 'undefined' will delete the item
1614 * from the array. Both of these changes imply that the size of the array may not
1615 * be the same size upon completion, as it was when it started.
1620 * @param Array array The Array to translate.
1621 * @param Function fn The function to process each item against.
1623 map: function(a,f) {
1624 // If a string is passed in for the function, make a function
1625 // for it (a handy shortcut)
1626 if ( f.constructor == String )
1627 f = new Function("a","return " + f);
1631 // Go through the array, translating each of the items to their
1632 // new value (or values).
1633 for ( var i = 0; i < a.length; i++ ) {
1635 if ( t !== null && t != undefined ) {
1636 if ( t.constructor != Array ) t = [t];
1637 r = jQuery.merge( t, r );
1644 * A number of helper functions used for managing events.
1645 * Many of the ideas behind this code orignated from Dean Edwards' addEvent library.
1649 // Bind an event to an element
1650 // Original by Dean Edwards
1651 add: function(element, type, handler) {
1652 // For whatever reason, IE has trouble passing the window object
1653 // around, causing it to be cloned in the process
1654 if ( jQuery.browser.msie && element.setInterval != undefined )
1657 // Make sure that the function being executed has a unique ID
1658 if ( !handler.guid )
1659 handler.guid = this.guid++;
1661 // Init the element's event structure
1662 if (!element.events)
1663 element.events = {};
1665 // Get the current list of functions bound to this event
1666 var handlers = element.events[type];
1668 // If it hasn't been initialized yet
1670 // Init the event handler queue
1671 handlers = element.events[type] = {};
1673 // Remember an existing handler, if it's already there
1674 if (element["on" + type])
1675 handlers[0] = element["on" + type];
1678 // Add the function to the element's handler list
1679 handlers[handler.guid] = handler;
1681 // And bind the global event handler to the element
1682 element["on" + type] = this.handle;
1684 // Remember the function in a global list (for triggering)
1685 if (!this.global[type])
1686 this.global[type] = [];
1687 this.global[type].push( element );
1693 // Detach an event or set of events from an element
1694 remove: function(element, type, handler) {
1696 if (type && element.events[type])
1698 delete element.events[type][handler.guid];
1700 for ( var i in element.events[type] )
1701 delete element.events[type][i];
1703 for ( var j in element.events )
1704 this.remove( element, j );
1707 trigger: function(type,data,element) {
1708 // Touch up the incoming data
1711 // Handle a global trigger
1713 var g = this.global[type];
1715 for ( var i = 0; i < g.length; i++ )
1716 this.trigger( type, data, g[i] );
1718 // Handle triggering a single element
1719 } else if ( element["on" + type] ) {
1720 // Pass along a fake event
1721 data.unshift( this.fix({ type: type, target: element }) );
1723 // Trigger the event
1724 element["on" + type].apply( element, data );
1728 handle: function(event) {
1729 if ( typeof jQuery == "undefined" ) return;
1731 event = event || jQuery.event.fix( window.event );
1733 // If no correct event was found, fail
1734 if ( !event ) return;
1736 var returnValue = true;
1738 var c = this.events[event.type];
1740 for ( var j in c ) {
1741 if ( c[j].apply( this, [event] ) === false ) {
1742 event.preventDefault();
1743 event.stopPropagation();
1744 returnValue = false;
1751 fix: function(event) {
1753 event.preventDefault = function() {
1754 this.returnValue = false;
1757 event.stopPropagation = function() {
1758 this.cancelBubble = true;