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 )
23 return $(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 );
46 // Watch for when a jQuery object is passed as the selector
50 // Watch for when a jQuery object is passed at the context
52 return $(c.get()).find(a);
54 // If the context is global, return a new object
56 return new jQuery(a,c);
58 // Watch for when an array is passed in
59 this.get( a.constructor == Array ?
60 // Assume that it's an array of DOM Elements
63 // Find the matching elements and save them for later
64 jQuery.Select( a, c ) );
66 var fn = arguments[ arguments.length - 1 ];
67 if ( fn && fn.constructor == Function )
71 // Map over the $ in case of overwrite
75 // Map the jQuery namespace to the '$' one
78 jQuery.fn = jQuery.prototype = {
80 * The current SVN version of jQuery.
90 * The number of elements currently matched.
98 * The number of elements currently matched.
108 * Access all matched elements. This serves as a backwards-compatible
109 * way of accessing all matched elements (other than the jQuery object
110 * itself, which is, in fact, an array of elements).
113 * @type Array<Element>
117 * Access a single matched element. <tt>num</tt> is used to access the
118 * <tt>num</tt>th element matched.
122 * @param Number num Access the element in the <tt>num</tt>th position.
126 * Set the jQuery object to an array of elements.
131 * @param Elements elems An array of elements
133 get: function( num ) {
134 // Watch for when an array (of elements) is passed in
135 if ( num && num.constructor == Array ) {
137 // Use a tricky hack to make the jQuery object
138 // look and feel like an array
140 [].push.apply( this, num );
144 return num == undefined ?
146 // Return a 'clean' array
147 $.map( this, function(a){ return a } ) :
149 // Return just the object
154 * Execute a function within the context of every matched element.
155 * This means that every time the passed-in function is executed
156 * (which is once for every element matched) the 'this' keyword
157 * points to the specific element.
159 * Additionally, the function, when executed, is passed a single
160 * argument representing the position of the element in the matched
165 * @param Function fn A function to execute
167 each: function( fn ) {
168 // Iterate through all of the matched elements
169 for ( var i = 0; i < this.length; i++ )
171 // Execute the function within the context of each element
172 fn.apply( this[i], [i] );
178 * Access a property on the first matched element.
179 * This method makes it easy to retreive a property value
180 * from the first matched element.
184 * @param String name The name of the property to access.
188 * Set a hash of key/value object properties to all matched elements.
189 * This serves as the best way to set a large number of properties
190 * on all matched elements.
194 * @param Hash prop A set of key/value pairs to set as object properties.
198 * Set a single property to a value, on all matched elements.
202 * @param String key The name of the property to set.
203 * @param Object value The value to set the property to.
205 attr: function( key, value ) {
206 // Check to see if we're setting style values
207 return key.constructor != String || value ?
208 this.each(function(){
209 // See if we're setting a hash of styles
210 if ( value == undefined )
211 // Set all the styles
212 for ( var prop in key )
214 type ? this.style : this,
218 // See if we're setting a single key/value style
221 type ? this.style : this,
226 // Look for the case where we're accessing a style value
227 jQuery[ type || "attr" ]( this[0], key );
231 * Access a style property on the first matched element.
232 * This method makes it easy to retreive a style property value
233 * from the first matched element.
237 * @param String name The name of the property to access.
241 * Set a hash of key/value style properties to all matched elements.
242 * This serves as the best way to set a large number of style properties
243 * on all matched elements.
247 * @param Hash prop A set of key/value pairs to set as style properties.
251 * Set a single style property to a value, on all matched elements.
255 * @param String key The name of the property to set.
256 * @param Object value The value to set the property to.
258 css: function( key, value ) {
259 return this.attr( key, value, "css" );
263 * Retreive the text contents of all matched elements. The result is
264 * a string that contains the combined text contents of all matched
265 * elements. This method works on both HTML and XML documents.
273 for ( var j = 0; j < e.length; j++ ) {
274 var r = e[j].childNodes;
275 for ( var i = 0; i < r.length; i++ )
276 t += r[i].nodeType != 1 ?
277 r[i].nodeValue : jQuery.fn.text([ r[i] ]);
283 * Set a single style property to a value, on all matched elements.
287 * @any String html A string of HTML, that will be created on the fly and wrapped around the target.
288 * @any Element elem A DOM element that will be wrapped.
289 * @any Array<Element> elems An array of elements, the first of which will be wrapped.
293 var a = jQuery.clean(arguments);
294 return this.each(function(){
295 var b = a[0].cloneNode(true);
296 this.parentNode.insertBefore( b, this );
297 while ( b.firstChild )
299 b.appendChild( this );
304 return this.domManip(arguments, true, 1, function(a){
305 this.appendChild( 1 );
309 prepend: function() {
310 return this.domManip(arguments, true, -1, function(a){
311 this.insertBefore( a, this.firstChild );
316 return this.domManip(arguments, false, 1, function(a){
317 this.parentNode.insertBefore( a, this );
322 return this.domManip(arguments, false, -1, function(a){
323 this.parentNode.insertBefore( a, this.nextSibling );
328 * A wrapper function for each() to be used by append and prepend.
329 * Handles cases where you're trying to modify the inner contents of
330 * a table, when you actually need to work with the tbody.
334 * @param {Function} fn The function doing the DOM manipulation.
337 domManip: function(args, table, dir, fn){
338 var clone = this.size() > 1;
339 var a = jQuery.clean(args);
341 return this.each(function(){
344 if ( table && this.nodeName == "TABLE" ) {
345 var tbody = this.getElementsByTagName("tbody");
347 if ( !tbody.length ) {
348 obj = document.createElement("tbody");
349 this.appendChild( obj );
354 for ( var i = ( dir < 0 ? a.length - 1 : 0 );
355 i != ( dir < 0 ? dir : a.length ); i += dir )
356 fn.apply( obj, [ a[i] ] );
360 pushStack: function(a,args) {
361 var fn = args && args[args.length-1];
363 if ( !fn || fn.constructor != Function ) {
364 if ( !this.stack ) this.stack = [];
365 this.stack.push( this.get() );
368 var old = this.get();
370 if ( fn.constructor == Function )
371 return this.each( fn );
379 this.get( this.stack.pop() );
384 return this.pushStack( jQuery.map( this, function(a){
385 return jQuery.Select(t,a);
389 filter: function(t) {
390 return t.constructor == Array ?
392 this.pushStack( jQuery.map(this,function(a){
393 for ( var i = 0; i < t.length; i++ )
394 if ( jQuery.filter(t[i],[a]).r.length )
398 this.pushStack( jQuery.filter(t,this).r, arguments );
401 return this.pushStack( t.constructor == String ?
402 jQuery.filter(t,this,false).r :
403 jQuery.grep(this,function(a){ return a != t; }), arguments );
406 return this.pushStack( jQuery.merge( this, t.constructor == String ?
407 jQuery.Select(t) : t.constructor == Array ? t : [t] ), arguments );
411 * A wrapper function for each() to be used by append and prepend.
412 * Handles cases where you're trying to modify the inner contents of
413 * a table, when you actually need to work with the tbody.
416 * @param {String} expr The expression with which to filter
420 return jQuery.filter(expr,this).r.length > 0;
425 var b = navigator.userAgent.toLowerCase();
427 // Figure out what browser is being used
429 ( /webkit/.test(b) && "safari" ) ||
430 ( /opera/.test(b) && "opera" ) ||
431 ( /msie/.test(b) && "msie" ) ||
432 ( !/compatible/.test(b) && "mozilla" ) ||
435 // Check to see if the W3C box model is being used
436 jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" );
439 parent: "a.parentNode",
440 parents: jQuery.parents,
441 ancestors: jQuery.parents,
442 next: "a.nextSibling",
443 prev: "a.previousSibling",
444 siblings: jQuery.sibling
447 for ( var i in axis ) {(function(){
449 jQuery.fn[ i ] = function(a) {
450 var ret = jQuery.map(this,t);
451 if ( a ) ret = jQuery.filter(a,ret).r;
452 return this.pushStack( ret, arguments );
456 var to = "html,append,prepend,before,after".split(',');
458 for ( var i = 0; i < to.length; i++ ) {(function(){
460 jQuery.fn[ n + "To" ] = function(){
462 return this.each(function(){
463 for ( var i = 0; i < a.length; i++ )
471 this.style.display = this.oldblock ? this.oldblock : "";
472 if ( jQuery.css(this,"display") == "none" )
473 this.style.display = "block";
477 this.oldblock = jQuery.css(this,"display");
478 if ( this.oldblock == "none" )
479 this.oldblock = "block";
480 this.style.display = "none";
484 var d = jQuery.css(this,"display");
485 $(this)[ !d || d == "none" ? 'show' : 'hide' ]();
488 addClass: function(c){
489 jQuery.className.add(this,c);
492 removeClass: function(c){
493 jQuery.className.remove(this,c);
496 toggleClass: function( c ){
497 jQuery.className[ jQuery.hasWord(this,a) ? 'remove' : 'add' ](this,c);
501 this.parentNode.removeChild( this );
505 while ( this.firstChild )
506 this.removeChild( this.firstChild );
509 bind: function( type, fn ) {
510 jQuery.event.add( this, type, fn );
513 unbind: function( type, fn ) {
514 jQuery.event.remove( this, type, fn );
517 trigger: function( type ) {
518 jQuery.event.trigger( this, type );
522 for ( var i in each ) {(function(){
524 jQuery.fn[ i ] = function(a,b) {
526 return this.each(function(){
537 for ( var i in attr ) {(function(){
539 jQuery.fn[ i ] = function(h) {
540 return h == undefined && this.length ?
541 this[0][n] : this.set( n, h );
549 if (jQuery.hasWord(o,c)) return;
550 o.className += ( o.className ? " " : "" ) + c;
552 remove: function(o,c){
553 o.className = !c ? "" :
555 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
559 $.swap = function(e,o,f) {
561 e.style["old"+i] = e.style[i];
566 e.style[i] = e.style["old"+i];
569 jQuery.css = function(e,p) {
570 // Adapted from Prototype 1.4.0
571 if ( p == "height" || p == "width" ) {
572 var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
575 old["padding" + d[i]] = 0;
576 old["border" + d[i] + "Width"] = 0;
579 $.swap( e, old, function() {
580 if (jQuery.css(e,"display") != 'none') {
581 oHeight = e.offsetHeight;
582 oWidth = e.offsetWidth;
584 $.swap( e, { visibility: 'hidden', position: 'absolute', display: '' },
586 oHeight = e.clientHeight;
587 oWidth = e.clientWidth;
591 return p == "height" ? oHeight : oWidth;
598 else if (e.currentStyle)
599 r = e.currentStyle[p];
600 else if (document.defaultView && document.defaultView.getComputedStyle) {
601 p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
602 var s = document.defaultView.getComputedStyle(e,"");
603 r = s ? s.getPropertyValue(p) : null;
609 jQuery.clean = function(a) {
611 for ( var i = 0; i < a.length; i++ ) {
612 if ( a[i].constructor == String ) {
614 if ( !a[i].indexOf("<tr") ) {
616 a[i] = "<table>" + a[i] + "</table>";
617 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
619 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
622 var div = document.createElement("div");
623 div.innerHTML = a[i];
626 div = div.firstChild.firstChild;
627 if ( td ) div = div.firstChild;
630 for ( var j = 0; j < div.childNodes.length; j++ )
631 r[r.length] = div.childNodes[j];
632 } else if ( a[i].length && !a[i].nodeType )
633 for ( var k = 0; k < a[i].length; k++ )
634 r[r.length] = a[i][k];
635 else if ( a[i] !== null )
637 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
643 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
644 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
651 last: "i==r.length-1",
654 "first-child": "jQuery.sibling(a,0).cur",
655 "nth-child": "(m[3]=='even'?jQuery.sibling(a,m[3]).n%2==0:(m[3]=='odd'?jQuery.sibling(a,m[3]).n%2==1:jQuery.sibling(a,m[3]).cur))",
656 "last-child": "jQuery.sibling(a,0,true).cur",
657 "nth-last-child": "jQuery.sibling(a,m[3],true).cur",
658 "first-of-type": "jQuery.ofType(a,0)",
659 "nth-of-type": "jQuery.ofType(a,m[3])",
660 "last-of-type": "jQuery.ofType(a,0,true)",
661 "nth-last-of-type": "jQuery.ofType(a,m[3],true)",
662 "only-of-type": "jQuery.ofType(a)==1",
663 "only-child": "jQuery.sibling(a).length==1",
664 parent: "a.childNodes.length",
665 empty: "!a.childNodes.length",
666 root: "a==(a.ownerDocument||document).documentElement",
667 contains: "(a.innerText||a.innerHTML).indexOf(m[3])!=-1",
668 visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!= 'hidden')",
669 hidden: "(a.type&&a.type=='hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'",
670 enabled: "!a.disabled",
671 disabled: "a.disabled",
674 ".": "jQuery.hasWord(a,m[2])",
676 "=": "jQuery.attr(a,m[3])==m[4]",
677 "!=": "jQuery.attr(a,m[3])!=m[4]",
678 "~=": "jQuery.hasWord(jQuery.attr(a,m[3]),m[4])",
679 "|=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
680 "^=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
681 "$=": "jQuery.attr(a,m[3]).substr( jQuery.attr(a,m[3]).length - m[4].length,m[4].length )==m[4]",
682 "*=": "jQuery.attr(a,m[3]).indexOf(m[4])>=0",
683 "": "m[3]=='*'?a.attributes.length>0:jQuery.attr(a,m[3])"
685 "[": "jQuery.Select(m[2],a).length"
689 "\\.\\.|/\\.\\.", "a.parentNode",
690 ">|/", "jQuery.sibling(a.firstChild)",
691 "\\+", "jQuery.sibling(a).next",
694 var s = jQuery.sibling(a);
696 for ( var i = s.n; i < s.length; i++ )
702 jQuery.Select = function( t, context ) {
703 // Make sure that the context is a DOM Element
704 if ( context && context.getElementsByTagName == undefined )
707 // Set the correct context (if none is provided)
708 context = context || jQuery.context || document;
710 if ( t.constructor != String ) return [t];
712 if ( !t.indexOf("//") ) {
713 context = context.documentElement;
714 t = t.substr(2,t.length);
715 } else if ( !t.indexOf("/") ) {
716 context = context.documentElement;
717 t = t.substr(1,t.length);
718 // FIX Assume the root element is right :(
719 if ( t.indexOf("/") >= 1 )
720 t = t.substr(t.indexOf("/"),t.length);
727 while ( t.length > 0 && last != t ) {
731 t = jQuery.cleanSpaces(t).replace( /^\/\//i, "" );
733 var foundToken = false;
735 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
736 var re = new RegExp("^(" + jQuery.token[i] + ")");
740 r = ret = jQuery.map( ret, jQuery.token[i+1] );
741 t = jQuery.cleanSpaces( t.replace( re, "" ) );
748 if ( !t.indexOf(",") || !t.indexOf("|") ) {
749 if ( ret[0] == context ) ret.shift();
750 done = jQuery.merge( done, ret );
752 t = " " + t.substr(1,t.length);
754 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
758 // Ummm, should make this work in all XML docs
759 var oid = document.getElementById(m[2]);
760 r = ret = oid ? [oid] : [];
761 t = t.replace( re2, "" );
763 if ( !m[2] || m[1] == "." ) m[2] = "*";
765 for ( var i = 0; i < ret.length; i++ )
768 jQuery.getAll(ret[i]) :
769 ret[i].getElementsByTagName(m[2])
777 var val = jQuery.filter(t,r);
779 t = jQuery.cleanSpaces(val.t);
783 if ( ret && ret[0] == context ) ret.shift();
784 done = jQuery.merge( done, ret );
789 jQuery.getAll = function(o,r) {
791 var s = o.childNodes;
792 for ( var i = 0; i < s.length; i++ )
793 if ( s[i].nodeType == 1 ) {
795 jQuery.getAll( s[i], r );
800 jQuery.attr = function(o,a,v){
801 if ( a && a.constructor == String ) {
804 "class": "className",
807 a = (fix[a] && fix[a].replace && fix[a]) || a;
808 var r = /-([a-z])/ig;
809 a = a.replace(r,function(z,b){return b.toUpperCase();});
810 if ( v != undefined ) {
812 if ( o.setAttribute && a != "disabled" )
815 return o[a] || o.getAttribute(a) || "";
820 jQuery.filter = function(t,r,not) {
823 g = function(a,f) {return jQuery.grep(a,f,true);};
825 while ( t && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
826 var re = /^\[ *@([a-z*_-][a-z0-9()_-]*) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
830 m = ["", "@", m[2], m[1], m[3]];
832 re = /^(\[) *([^\]]*) *\]/i;
836 re = /^(:)([a-z0-9*_-]*)\( *["']?([^ \)'"]*)['"]? *\)/i;
840 re = /^([:\.#]*)([a-z0-9*_-]*)/i;
845 t = t.replace( re, "" );
847 if ( m[1] == ":" && m[2] == "not" )
848 r = jQuery.filter(m[3],r,false).r;
852 if ( jQuery.g[m[1]].constructor == String )
854 else if ( jQuery.g[m[1]][m[2]] )
855 f = jQuery.g[m[1]][m[2]];
858 eval("f = function(a,i){return " + f + "}");
864 return { r: r, t: t };
867 jQuery.parents = function(a){
869 var c = a.parentNode;
870 while ( c && c != document ) {
877 jQuery.cleanSpaces = function(t){
878 return t.replace(/^\s+|\s+$/g, "");
881 jQuery.ofType = function(a,n,e) {
882 var t = jQuery.grep(jQuery.sibling(a),function(b){ return b.nodeName == a.nodeName; });
883 if ( e ) n = t.length - n - 1;
884 return n != undefined ? t[n] == a : t.length;
887 jQuery.sibling = function(a,n,e) {
889 var tmp = a.parentNode.childNodes;
890 for ( var i = 0; i < tmp.length; i++ ) {
891 if ( tmp[i].nodeType == 1 )
892 type[type.length] = tmp[i];
894 type.n = type.length - 1;
896 if ( e ) n = type.length - n - 1;
897 type.cur = type[n] == a;
898 type.next = type[type.n + 1];
902 jQuery.hasWord = function(e,a) {
905 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
908 jQuery.merge = function(a,b) {
910 for ( var k = 0; k < b.length; k++ ) d[k] = b[k];
912 for ( var i = 0; i < a.length; i++ ) {
914 for ( var j = 0; j < b.length; j++ )
917 if ( c ) d[d.length] = a[i];
923 jQuery.grep = function(a,f,s) {
924 if ( f.constructor == String )
925 f = new Function("a","i","return " + f);
928 for ( var i = 0; i < a.length; i++ )
929 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
934 jQuery.map = function(a,f) {
935 if ( f.constructor == String )
936 f = new Function("a","return " + f);
939 for ( var i = 0; i < a.length; i++ ) {
941 if ( t !== null && t != undefined ) {
942 if ( t.constructor != Array ) t = [t];
943 r = jQuery.merge( t, r );
951 // Bind an event to an element
952 // Original by Dean Edwards
953 add: function(element, type, handler) {
954 // For whatever reason, IE has trouble passing the window object
955 // around, causing it to be cloned in the process
956 if ( jQuery.browser == "msie" && element.setInterval != undefined )
959 if (!handler.guid) handler.guid = jQuery.event.guid++;
960 if (!element.events) element.events = {};
961 var handlers = element.events[type];
963 handlers = element.events[type] = {};
964 if (element["on" + type])
965 handlers[0] = element["on" + type];
967 handlers[handler.guid] = handler;
968 element["on" + type] = this.handle;
970 if (!this.global[type])
971 this.global[type] = [];
972 this.global[type].push( element );
978 // Detach an event or set of events from an element
979 remove: function(element, type, handler) {
981 if (type && element.events[type])
983 delete element.events[type][handler.guid];
985 for ( var i in element.events[type] )
986 delete element.events[type][i];
988 for ( var j in element.events )
989 this.remove( element, j );
992 trigger: function(type,data,element) {
993 // Touch up the incoming data
996 // Handle a global trigger
998 var g = this.global[type];
1000 for ( var i = 0; i < g.length; i++ )
1001 this.trigger( type, data, g[i] );
1003 // Handle triggering a single element
1004 } else if ( element["on" + type] ) {
1005 // Pass along a fake event
1006 data.unshift( this.fix({ type: type, target: element }) );
1008 // Trigger the event
1009 element["on" + type].apply( element, data );
1013 handle: function(event) {
1014 // Handle adding events to items in IFrames, in IE
1016 jQuery.event.fix( ((this.ownerDocument || this.document ||
1017 this).parentWindow || window).event );
1019 // If no correct event was found, fail
1020 if ( !event ) return;
1022 var returnValue = true, handlers = [];
1024 for ( var j in this.events[event.type] )
1025 handlers[handlers.length] = this.events[event.type][j];
1027 for ( var i = 0; i < handlers.length; i++ )
1028 if ( handlers[i].constructor == Function ) {
1029 this.handleEvent = handlers[i];
1030 if (this.handleEvent(event) === false) {
1031 event.preventDefault();
1032 event.stopPropagation();
1033 returnValue = false;
1039 fix: function(event) {
1041 event.preventDefault = function() {
1042 this.returnValue = false;
1045 event.stopPropagation = function() {
1046 this.cancelBubble = true;