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 * Handle support for overriding other $() functions. Way too many libraries
22 * provide this function to simply ignore it and overwrite it.
25 // Check to see if this is a possible collision case
26 if ( jQuery._$ && !c && a.constructor == String &&
28 // Make sure that the expression is a colliding one
29 !/[^a-zA-Z0-9_-]/.test(a) &&
31 // and that there are no elements that match it
32 // (this is the one truly ambiguous case)
33 !document.getElementsByTagName(a).length )
35 // Use the default method, in case it works some voodoo
36 return jQuery._$( a );
38 // Make sure t hat a selection was provided
39 a = a || jQuery.context || document;
41 // Watch for when a jQuery object is passed as the selector
45 // Watch for when a jQuery object is passed at the context
47 return $(c.get()).find(a);
49 // If the context is global, return a new object
51 return new jQuery(a,c);
53 // Watch for when an array is passed in
54 if ( a.constructor == Array )
55 // Assume that it's an array of DOM Elements
58 // Find the matching elements and save them for later
59 this.cur = jQuery.Select( a, c );
62 // Map over the $ in case of overwrite
66 // Map the jQuery namespace to the '$' one
69 jQuery.fn = jQuery.prototype = {
71 * The current SVN version of jQuery.
79 * The number of elements currently matched.
84 return this.get().length;
88 * Access the elements matched. If a number is provided,
89 * the Nth element is returned, otherwise, an array of all
90 * matched items is returned.
92 * @type Array,DOMElement
95 return num == undefined ? this.cur : this.cur[num];
99 for ( var i = 0; i < this.size(); i++ )
100 f.apply( this.get(i), [i] );
104 return this.each(function(){
105 if ( b === undefined )
107 jQuery.attr(this,j,a[j]);
109 jQuery.attr(this,a,b);
113 return h == undefined && this.size() ?
114 this.get(0).innerHTML : this.set( "innerHTML", h );
117 return h == undefined && this.size() ?
118 this.get(0).value : this.set( "value", h );
123 for ( var j = 0; j < e.length; j++ ) {
124 var r = e[j].childNodes;
125 for ( var i = 0; i < r.length; i++ )
126 t += r[i].nodeType != 1 ?
127 r[i].nodeValue : jQuery.fn.text([ r[i] ]);
133 return a.constructor != String || b ?
134 this.each(function(){
135 if ( b === undefined )
137 jQuery.attr(this.style,j,a[j]);
139 jQuery.attr(this.style,a,b);
140 }) : jQuery.css( this.get(0), a );
143 return this.each(function(){
144 var d = jQuery.css(this,"display");
145 if ( !d || d == "none" )
152 return this.each(function(){
153 this.style.display = this.oldblock ? this.oldblock : "";
154 if ( jQuery.css(this,"display") == "none" )
155 this.style.display = "block";
159 return this.each(function(){
160 this.oldblock = jQuery.css(this,"display");
161 if ( this.oldblock == "none" )
162 this.oldblock = "block";
163 this.style.display = "none";
166 addClass: function(c) {
167 return this.each(function(){
168 jQuery.className.add(this,c);
171 removeClass: function(c) {
172 return this.each(function(){
173 jQuery.className.remove(this,c);
177 toggleClass: function(c) {
178 return this.each(function(){
179 if (jQuery.hasWord(this,c))
180 jQuery.className.remove(this,c);
182 jQuery.className.add(this,c);
186 return this.each(function(){
187 this.parentNode.removeChild( this );
192 var a = jQuery.clean(arguments);
193 return this.each(function(){
194 var b = a[0].cloneNode(true);
195 this.parentNode.insertBefore( b, this );
196 while ( b.firstChild )
198 b.appendChild( this );
203 var clone = this.size() > 1;
204 var a = jQuery.clean(arguments);
205 return this.domManip(function(){
206 for ( var i = 0; i < a.length; i++ )
207 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
211 appendTo: function() {
213 return this.each(function(){
214 for ( var i = 0; i < a.length; i++ )
215 $(a[i]).append( this );
219 prepend: function() {
220 var clone = this.size() > 1;
221 var a = jQuery.clean(arguments);
222 return this.domManip(function(){
223 for ( var i = a.length - 1; i >= 0; i-- )
224 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
229 var clone = this.size() > 1;
230 var a = jQuery.clean(arguments);
231 return this.each(function(){
232 for ( var i = 0; i < a.length; i++ )
233 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
238 var clone = this.size() > 1;
239 var a = jQuery.clean(arguments);
240 return this.each(function(){
241 for ( var i = a.length - 1; i >= 0; i-- )
242 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
247 return this.each(function(){
248 while ( this.firstChild )
249 this.removeChild( this.firstChild );
253 bind: function(t,f) {
254 return this.each(function(){jQuery.event.add(this,t,f);});
256 unbind: function(t,f) {
257 return this.each(function(){jQuery.event.remove(this,t,f);});
259 trigger: function(t) {
260 return this.each(function(){jQuery.event.trigger(this,t);});
263 pushStack: function(a) {
264 if ( !this.stack ) this.stack = [];
265 this.stack.unshift( this.cur );
266 if ( a ) this.cur = a;
272 this.each(function(){
273 ret = jQuery.merge( ret, jQuery.Select(t,this) );
275 this.pushStack( ret );
280 this.cur = this.stack.shift();
284 parent: function(a) {
285 var ret = jQuery.map(this.cur,"a.parentNode");
286 if ( a ) ret = jQuery.filter(a,ret).r;
287 return this.pushStack(ret);
290 parents: function(a) {
291 var ret = jQuery.map(this.cur,jQuery.parents);
292 if ( a ) ret = jQuery.filter(a,ret).r;
293 return this.pushStack(ret);
296 siblings: function(a) {
297 // Incorrect, need to exclude current element
298 var ret = jQuery.map(this.cur,jQuery.sibling);
299 if ( a ) ret = jQuery.filter(a,ret).r;
300 return this.pushStack(ret);
303 filter: function(t) {
305 var p = t.split(/\s*,\s*/);
306 return this.pushStack( $.map(this.cur,function(a){
307 for ( var i = 0; i < p.length; i++ )
308 if ( jQuery.filter(p[i],[a]).r.length )
312 return this.pushStack( jQuery.filter(t,this.cur).r );
315 return this.pushStack( t.constructor == String ?
316 jQuery.filter(t,this.cur,false).r :
317 jQuery.grep(this.cur,function(a){ return a != t; }) );
320 return this.pushStack( jQuery.merge( this.cur, t.constructor == String ?
321 jQuery.Select(t) : t.constructor == Array ? t : [t] ) );
325 * A wrapper function for each() to be used by append and prepend.
326 * Handles cases where you're trying to modify the inner contents of
327 * a table, when you actually need to work with the tbody.
330 * @param {String} expr The expression with which to filter
334 return jQuery.filter(expr,this.cur).r.length > 0;
338 * A wrapper function for each() to be used by append and prepend.
339 * Handles cases where you're trying to modify the inner contents of
340 * a table, when you actually need to work with the tbody.
344 * @param {Function} fn The function doing the DOM manipulation.
347 domManip: function(fn){
348 return this.each(function(){
351 if ( this.nodeName == "TABLE" ) {
352 var tbody = this.getElementsByTagName("tbody");
354 if ( !tbody.length ) {
355 obj = document.createElement("tbody");
356 this.appendChild( obj );
367 * Similar to the Prototype $A() function, only it allows you to
368 * forcefully pass array-like structures into $().
370 jQuery.A = function(a){
371 // Create a temporary, clean, array
374 // and copy the old array contents over to it
375 for ( var i = 0; i < a.length; i++ )
378 // Return the sane jQuery object
384 if (jQuery.hasWord(o,c)) return;
385 o.className += ( o.className ? " " : "" ) + c;
387 remove: function(o,c){
388 o.className = !c ? "" :
390 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
395 var b = navigator.userAgent.toLowerCase();
397 // Figure out what browser is being used
399 ( /webkit/.test(b) && "safari" ) ||
400 ( /opera/.test(b) && "opera" ) ||
401 ( /msie/.test(b) && "msie" ) ||
402 ( !/compatible/.test(b) && "mozilla" ) ||
405 // Check to see if the W3C box model is being used
406 jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" );
409 $.swap = function(e,o,f) {
411 e.style["old"+i] = e.style[i];
416 e.style[i] = e.style["old"+i];
419 jQuery.css = function(e,p) {
420 // Adapted from Prototype 1.4.0
421 if ( p == "height" || p == "width" ) {
422 var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
425 old["padding" + d[i]] = 0;
426 old["border" + d[i] + "Width"] = 0;
429 $.swap( e, old, function() {
430 if (jQuery.css(e,"display") != 'none') {
431 oHeight = e.offsetHeight;
432 oWidth = e.offsetWidth;
434 $.swap( e, { visibility: 'hidden', position: 'absolute', display: '' },
436 oHeight = e.clientHeight;
437 oWidth = e.clientWidth;
441 return p == "height" ? oHeight : oWidth;
448 else if (e.currentStyle)
449 r = e.currentStyle[p];
450 else if (document.defaultView && document.defaultView.getComputedStyle) {
451 p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
452 var s = document.defaultView.getComputedStyle(e,"");
453 r = s ? s.getPropertyValue(p) : null;
459 jQuery.clean = function(a) {
461 for ( var i = 0; i < a.length; i++ ) {
462 if ( a[i].constructor == String ) {
464 if ( !a[i].indexOf("<tr") ) {
466 a[i] = "<table>" + a[i] + "</table>";
467 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
469 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
472 var div = document.createElement("div");
473 div.innerHTML = a[i];
476 div = div.firstChild.firstChild;
477 if ( td ) div = div.firstChild;
480 for ( var j = 0; j < div.childNodes.length; j++ )
481 r[r.length] = div.childNodes[j];
482 } else if ( a[i].length && !a[i].nodeType )
483 for ( var k = 0; k < a[i].length; k++ )
484 r[r.length] = a[i][k];
485 else if ( a[i] !== null )
487 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
493 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
494 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
501 last: "i==r.length-1",
504 "first-child": "jQuery.sibling(a,0).cur",
505 "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))",
506 "last-child": "jQuery.sibling(a,0,true).cur",
507 "nth-last-child": "jQuery.sibling(a,m[3],true).cur",
508 "first-of-type": "jQuery.ofType(a,0)",
509 "nth-of-type": "jQuery.ofType(a,m[3])",
510 "last-of-type": "jQuery.ofType(a,0,true)",
511 "nth-last-of-type": "jQuery.ofType(a,m[3],true)",
512 "only-of-type": "jQuery.ofType(a)==1",
513 "only-child": "jQuery.sibling(a).length==1",
514 parent: "a.childNodes.length",
515 empty: "!a.childNodes.length",
516 root: "a==(a.ownerDocument||document).documentElement",
517 contains: "(a.innerText||a.innerHTML).indexOf(m[3])!=-1",
518 visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!= 'hidden')",
519 hidden: "(a.type&&a.type=='hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'",
520 enabled: "!a.disabled",
521 disabled: "a.disabled",
524 ".": "jQuery.hasWord(a,m[2])",
526 "=": "jQuery.attr(a,m[3])==m[4]",
527 "!=": "jQuery.attr(a,m[3])!=m[4]",
528 "~=": "jQuery.hasWord(jQuery.attr(a,m[3]),m[4])",
529 "|=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
530 "^=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
531 "$=": "jQuery.attr(a,m[3]).substr( jQuery.attr(a,m[3]).length - m[4].length,m[4].length )==m[4]",
532 "*=": "jQuery.attr(a,m[3]).indexOf(m[4])>=0",
533 "": "m[3]=='*'?a.attributes.length>0:jQuery.attr(a,m[3])"
535 "[": "jQuery.Select(m[2],a).length"
539 "\\.\\.|/\\.\\.", "a.parentNode",
540 ">|/", "jQuery.sibling(a.firstChild)",
541 "\\+", "jQuery.sibling(a).next",
544 var s = jQuery.sibling(a);
546 for ( var i = s.n; i < s.length; i++ )
552 jQuery.Select = function( t, context ) {
553 context = context || jQuery.context || document;
555 if ( t.constructor != String ) return [t];
557 if ( !t.indexOf("//") ) {
558 context = context.documentElement;
559 t = t.substr(2,t.length);
560 } else if ( !t.indexOf("/") ) {
561 context = context.documentElement;
562 t = t.substr(1,t.length);
563 // FIX Assume the root element is right :(
564 if ( t.indexOf("/") >= 1 )
565 t = t.substr(t.indexOf("/"),t.length);
572 while ( t.length > 0 && last != t ) {
576 t = jQuery.cleanSpaces(t).replace( /^\/\//i, "" );
578 var foundToken = false;
580 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
581 var re = new RegExp("^(" + jQuery.token[i] + ")");
585 r = ret = jQuery.map( ret, jQuery.token[i+1] );
586 t = jQuery.cleanSpaces( t.replace( re, "" ) );
593 if ( !t.indexOf(",") || !t.indexOf("|") ) {
594 if ( ret[0] == context ) ret.shift();
595 done = jQuery.merge( done, ret );
597 t = " " + t.substr(1,t.length);
599 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
603 // Ummm, should make this work in all XML docs
604 var oid = document.getElementById(m[2]);
605 r = ret = oid ? [oid] : [];
606 t = t.replace( re2, "" );
608 if ( !m[2] || m[1] == "." ) m[2] = "*";
610 for ( var i = 0; i < ret.length; i++ )
613 jQuery.getAll(ret[i]) :
614 ret[i].getElementsByTagName(m[2])
622 var val = jQuery.filter(t,r);
624 t = jQuery.cleanSpaces(val.t);
628 if ( ret && ret[0] == context ) ret.shift();
629 done = jQuery.merge( done, ret );
634 jQuery.getAll = function(o,r) {
636 var s = o.childNodes;
637 for ( var i = 0; i < s.length; i++ )
638 if ( s[i].nodeType == 1 ) {
640 jQuery.getAll( s[i], r );
645 jQuery.attr = function(o,a,v){
646 if ( a && a.constructor == String ) {
649 "class": "className",
652 a = (fix[a] && fix[a].replace && fix[a]) || a;
653 var r = /-([a-z])/ig;
654 a = a.replace(r,function(z,b){return b.toUpperCase();});
655 if ( v != undefined ) {
657 if ( o.setAttribute && a != "disabled" )
660 return o[a] || o.getAttribute(a) || "";
665 jQuery.filter = function(t,r,not) {
668 g = function(a,f) {return jQuery.grep(a,f,true);};
670 while ( t && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
671 var re = /^\[ *@([a-z0-9*()_-]+) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
675 m = ["", "@", m[2], m[1], m[3]];
677 re = /^(\[) *([^\]]*) *\]/i;
681 re = /^(:)([a-z0-9*_-]*)\( *["']?([^ \)'"]*)['"]? *\)/i;
685 re = /^([:\.#]*)([a-z0-9*_-]*)/i;
690 t = t.replace( re, "" );
692 if ( m[1] == ":" && m[2] == "not" )
693 r = jQuery.filter(m[3],r,false).r;
697 if ( jQuery.g[m[1]].constructor == String )
699 else if ( jQuery.g[m[1]][m[2]] )
700 f = jQuery.g[m[1]][m[2]];
703 eval("f = function(a,i){return " + f + "}");
709 return { r: r, t: t };
712 jQuery.parents = function(a){
714 var c = a.parentNode;
715 while ( c && c != document ) {
722 jQuery.cleanSpaces = function(t){
723 return t.replace(/^\s+|\s+$/g, "");
726 jQuery.ofType = function(a,n,e) {
727 var t = jQuery.grep(jQuery.sibling(a),function(b){ return b.nodeName == a.nodeName; });
728 if ( e ) n = t.length - n - 1;
729 return n != undefined ? t[n] == a : t.length;
732 jQuery.sibling = function(a,n,e) {
734 var tmp = a.parentNode.childNodes;
735 for ( var i = 0; i < tmp.length; i++ ) {
736 if ( tmp[i].nodeType == 1 )
737 type[type.length] = tmp[i];
739 type.n = type.length - 1;
741 if ( e ) n = type.length - n - 1;
742 type.cur = ( type[n] == a );
743 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
744 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
748 jQuery.hasWord = function(e,a) {
749 if ( e == undefined ) return;
750 if ( e.className ) e = e.className;
751 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
754 jQuery.merge = function(a,b) {
756 for ( var k = 0; k < b.length; k++ ) d[k] = b[k];
758 for ( var i = 0; i < a.length; i++ ) {
760 for ( var j = 0; j < b.length; j++ )
763 if ( c ) d[d.length] = a[i];
769 jQuery.grep = function(a,f,s) {
770 if ( f.constructor == String )
771 f = new Function("a","i","return " + f);
774 for ( var i = 0; i < a.length; i++ )
775 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
780 jQuery.map = function(a,f) {
781 if ( f.constructor == String )
782 f = new Function("a","return " + f);
785 for ( var i = 0; i < a.length; i++ ) {
787 if ( t !== null && t != undefined ) {
788 if ( t.constructor != Array ) t = [t];
789 r = jQuery.merge( t, r );
797 // Bind an event to an element
798 // Original by Dean Edwards
799 add: function(element, type, handler) {
800 // For whatever reason, IE has trouble passing the window object
801 // around, causing it to be cloned in the process
802 if ( jQuery.browser == "msie" && element.setInterval != undefined )
805 if (!handler.guid) handler.guid = jQuery.event.guid++;
806 if (!element.events) element.events = {};
807 var handlers = element.events[type];
809 handlers = element.events[type] = {};
810 if (element["on" + type])
811 handlers[0] = element["on" + type];
813 handlers[handler.guid] = handler;
814 element["on" + type] = this.handle;
816 if (!this.global[type])
817 this.global[type] = [];
818 this.global[type].push( element );
824 // Detach an event or set of events from an element
825 remove: function(element, type, handler) {
827 if (type && element.events[type])
829 delete element.events[type][handler.guid];
831 for ( var i in element.events[type] )
832 delete element.events[type][i];
834 for ( var j in element.events )
835 this.remove( element, j );
838 trigger: function(type,data,element) {
839 // Touch up the incoming data
842 // Handle a global trigger
844 var g = this.global[type];
846 for ( var i = 0; i < g.length; i++ )
847 this.trigger( type, data, g[i] );
849 // Handle triggering a single element
850 } else if ( element["on" + type] ) {
851 // Pass along a fake event
852 data.unshift( this.fix({ type: type, target: element }) );
855 element["on" + type].apply( element, data );
859 handle: function(event) {
860 // Handle adding events to items in IFrames, in IE
862 jQuery.event.fix( ((this.ownerDocument || this.document ||
863 this).parentWindow || window).event );
865 // If no correct event was found, fail
866 if ( !event ) return;
868 var returnValue = true, handlers = [];
870 for ( var j in this.events[event.type] )
871 handlers[handlers.length] = this.events[event.type][j];
873 for ( var i = 0; i < handlers.length; i++ )
874 if ( handlers[i].constructor == Function ) {
875 this.handleEvent = handlers[i];
876 if (this.handleEvent(event) === false) {
877 event.preventDefault();
878 event.stopPropagation();
885 fix: function(event) {
887 event.preventDefault = function() {
888 this.returnValue = false;
891 event.stopPropagation = function() {
892 this.cancelBubble = true;