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 // Make sure t hat a selection was provided
22 a = a || jQuery.context || document;
25 * Handle support for overriding other $() functions. Way too many libraries
26 * provide this function to simply ignore it and overwrite it.
29 // Check to see if this is a possible collision case
30 if ( jQuery._$ && !c && a.constructor == String &&
32 // Make sure that the expression is a colliding one
33 !/[^a-zA-Z0-9_-]/.test(a) &&
35 // and that there are no elements that match it
36 // (this is the one truly ambiguous case)
37 !document.getElementsByTagName(a).length )
39 // Use the default method, in case it works some voodoo
40 return jQuery._$( a );
42 // Watch for when a jQuery object is passed as the selector
46 // Watch for when a jQuery object is passed at the context
48 return $(c.get()).find(a);
50 // If the context is global, return a new object
52 return new jQuery(a,c);
54 // Watch for when an array is passed in
55 this.get( a.constructor == Array ?
56 // Assume that it's an array of DOM Elements
59 // Find the matching elements and save them for later
60 jQuery.Select( a, c ) );
62 var fn = arguments[ arguments.length - 1 ];
63 if ( fn && fn.constructor == Function )
67 // Map over the $ in case of overwrite
71 // Map the jQuery namespace to the '$' one
74 jQuery.fn = jQuery.prototype = {
76 * The current SVN version of jQuery.
84 * The number of elements currently matched.
93 * Access the elements matched. If a number is provided,
94 * the Nth element is returned, otherwise, an array of all
95 * matched items is returned.
97 * @type Array,DOMElement
100 if ( num && num.constructor == Array ) {
102 [].push.apply( this, num );
104 return num == undefined ?
106 // Return a 'clean' array
107 $.map( this, function(a){ return a } ) :
109 // Return just the object
114 for ( var i = 0; i < this.size(); i++ )
115 f.apply( this[i], [i] );
119 return this.each(function(){
120 if ( b === undefined )
122 jQuery.attr(this,j,a[j]);
124 jQuery.attr(this,a,b);
128 return h == undefined && this.size() ?
129 this[0].innerHTML : this.set( "innerHTML", h );
132 return h == undefined && this.size() ?
133 this[0].value : this.set( "value", h );
138 for ( var j = 0; j < e.length; j++ ) {
139 var r = e[j].childNodes;
140 for ( var i = 0; i < r.length; i++ )
141 t += r[i].nodeType != 1 ?
142 r[i].nodeValue : jQuery.fn.text([ r[i] ]);
148 return a.constructor != String || b ?
149 this.each(function(){
150 if ( b === undefined )
152 jQuery.attr(this.style,j,a[j]);
154 jQuery.attr(this.style,a,b);
155 }) : jQuery.css( this[0], a );
158 return this.each(function(){
159 var d = jQuery.css(this,"display");
160 if ( !d || d == "none" )
167 return this.each(function(){
168 this.style.display = this.oldblock ? this.oldblock : "";
169 if ( jQuery.css(this,"display") == "none" )
170 this.style.display = "block";
174 return this.each(function(){
175 this.oldblock = jQuery.css(this,"display");
176 if ( this.oldblock == "none" )
177 this.oldblock = "block";
178 this.style.display = "none";
181 addClass: function(c) {
182 return this.each(function(){
183 jQuery.className.add(this,c);
186 removeClass: function(c) {
187 return this.each(function(){
188 jQuery.className.remove(this,c);
192 toggleClass: function(c) {
193 return this.each(function(){
194 if (jQuery.hasWord(this,c))
195 jQuery.className.remove(this,c);
197 jQuery.className.add(this,c);
201 return this.each(function(){
202 this.parentNode.removeChild( this );
207 var a = jQuery.clean(arguments);
208 return this.each(function(){
209 var b = a[0].cloneNode(true);
210 this.parentNode.insertBefore( b, this );
211 while ( b.firstChild )
213 b.appendChild( this );
218 var clone = this.size() > 1;
219 var a = jQuery.clean(arguments);
220 return this.domManip(function(){
221 for ( var i = 0; i < a.length; i++ )
222 this.appendChild( clone ? a[i].cloneNode(true) : a[i] );
226 appendTo: function() {
228 return this.each(function(){
229 for ( var i = 0; i < a.length; i++ )
230 $(a[i]).append( this );
234 prepend: function() {
235 var clone = this.size() > 1;
236 var a = jQuery.clean(arguments);
237 return this.domManip(function(){
238 for ( var i = a.length - 1; i >= 0; i-- )
239 this.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.firstChild );
244 var clone = this.size() > 1;
245 var a = jQuery.clean(arguments);
246 return this.each(function(){
247 for ( var i = 0; i < a.length; i++ )
248 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this );
253 var clone = this.size() > 1;
254 var a = jQuery.clean(arguments);
255 return this.each(function(){
256 for ( var i = a.length - 1; i >= 0; i-- )
257 this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this.nextSibling );
262 return this.each(function(){
263 while ( this.firstChild )
264 this.removeChild( this.firstChild );
268 bind: function(t,f) {
269 return this.each(function(){jQuery.event.add(this,t,f);});
271 unbind: function(t,f) {
272 return this.each(function(){jQuery.event.remove(this,t,f);});
274 trigger: function(t) {
275 return this.each(function(){jQuery.event.trigger(this,t);});
278 pushStack: function(a,args) {
279 var fn = args[args.length-1];
281 if ( !fn || fn.constructor != Function ) {
282 if ( !this.stack ) this.stack = [];
283 this.stack.push( this.get() );
286 var old = this.get();
288 if ( fn.constructor == Function )
289 return this.each( fn );
297 this.get( this.stack.pop() );
302 return this.pushStack( $.map( this, function(a){
303 return jQuery.Select(t,a);
307 parent: function(a) {
308 var ret = jQuery.map(this,"a.parentNode");
309 if ( a ) ret = jQuery.filter(a,ret).r;
310 return this.pushStack( ret, arguments );
313 parents: function(a) {
314 var ret = jQuery.map(this,jQuery.parents);
315 if ( a ) ret = jQuery.filter(a,ret).r;
316 return this.pushStack( ret, arguments );
319 siblings: function(a) {
320 // Incorrect, need to exclude current element
321 var ret = jQuery.map(this,jQuery.sibling);
322 if ( a ) ret = jQuery.filter(a,ret).r;
323 return this.pushStack( ret, arguments );
326 filter: function(t) {
328 var p = t.split(/\s*,\s*/);
329 return this.pushStack( $.map(this,function(a){
330 for ( var i = 0; i < p.length; i++ )
331 if ( jQuery.filter(p[i],[a]).r.length )
335 return this.pushStack( jQuery.filter(t,this).r, arguments );
338 return this.pushStack( t.constructor == String ?
339 jQuery.filter(t,this,false).r :
340 jQuery.grep(this,function(a){ return a != t; }), arguments );
343 return this.pushStack( jQuery.merge( this, t.constructor == String ?
344 jQuery.Select(t) : t.constructor == Array ? t : [t] ), arguments );
348 * A wrapper function for each() to be used by append and prepend.
349 * Handles cases where you're trying to modify the inner contents of
350 * a table, when you actually need to work with the tbody.
353 * @param {String} expr The expression with which to filter
357 return jQuery.filter(expr,this).r.length > 0;
361 * A wrapper function for each() to be used by append and prepend.
362 * Handles cases where you're trying to modify the inner contents of
363 * a table, when you actually need to work with the tbody.
367 * @param {Function} fn The function doing the DOM manipulation.
370 domManip: function(fn){
371 return this.each(function(){
374 if ( this.nodeName == "TABLE" ) {
375 var tbody = this.getElementsByTagName("tbody");
377 if ( !tbody.length ) {
378 obj = document.createElement("tbody");
379 this.appendChild( obj );
390 * Similar to the Prototype $A() function, only it allows you to
391 * forcefully pass array-like structures into $().
393 jQuery.A = function(a){
394 // Create a temporary, clean, array
397 // and copy the old array contents over to it
398 for ( var i = 0; i < a.length; i++ )
401 // Return the sane jQuery object
407 if (jQuery.hasWord(o,c)) return;
408 o.className += ( o.className ? " " : "" ) + c;
410 remove: function(o,c){
411 o.className = !c ? "" :
413 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
418 var b = navigator.userAgent.toLowerCase();
420 // Figure out what browser is being used
422 ( /webkit/.test(b) && "safari" ) ||
423 ( /opera/.test(b) && "opera" ) ||
424 ( /msie/.test(b) && "msie" ) ||
425 ( !/compatible/.test(b) && "mozilla" ) ||
428 // Check to see if the W3C box model is being used
429 jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" );
432 $.swap = function(e,o,f) {
434 e.style["old"+i] = e.style[i];
439 e.style[i] = e.style["old"+i];
442 jQuery.css = function(e,p) {
443 // Adapted from Prototype 1.4.0
444 if ( p == "height" || p == "width" ) {
445 var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
448 old["padding" + d[i]] = 0;
449 old["border" + d[i] + "Width"] = 0;
452 $.swap( e, old, function() {
453 if (jQuery.css(e,"display") != 'none') {
454 oHeight = e.offsetHeight;
455 oWidth = e.offsetWidth;
457 $.swap( e, { visibility: 'hidden', position: 'absolute', display: '' },
459 oHeight = e.clientHeight;
460 oWidth = e.clientWidth;
464 return p == "height" ? oHeight : oWidth;
471 else if (e.currentStyle)
472 r = e.currentStyle[p];
473 else if (document.defaultView && document.defaultView.getComputedStyle) {
474 p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
475 var s = document.defaultView.getComputedStyle(e,"");
476 r = s ? s.getPropertyValue(p) : null;
482 jQuery.clean = function(a) {
484 for ( var i = 0; i < a.length; i++ ) {
485 if ( a[i].constructor == String ) {
487 if ( !a[i].indexOf("<tr") ) {
489 a[i] = "<table>" + a[i] + "</table>";
490 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
492 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
495 var div = document.createElement("div");
496 div.innerHTML = a[i];
499 div = div.firstChild.firstChild;
500 if ( td ) div = div.firstChild;
503 for ( var j = 0; j < div.childNodes.length; j++ )
504 r[r.length] = div.childNodes[j];
505 } else if ( a[i].length && !a[i].nodeType )
506 for ( var k = 0; k < a[i].length; k++ )
507 r[r.length] = a[i][k];
508 else if ( a[i] !== null )
510 a[i].nodeType ? a[i] : document.createTextNode(a[i].toString());
516 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
517 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
524 last: "i==r.length-1",
527 "first-child": "jQuery.sibling(a,0).cur",
528 "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))",
529 "last-child": "jQuery.sibling(a,0,true).cur",
530 "nth-last-child": "jQuery.sibling(a,m[3],true).cur",
531 "first-of-type": "jQuery.ofType(a,0)",
532 "nth-of-type": "jQuery.ofType(a,m[3])",
533 "last-of-type": "jQuery.ofType(a,0,true)",
534 "nth-last-of-type": "jQuery.ofType(a,m[3],true)",
535 "only-of-type": "jQuery.ofType(a)==1",
536 "only-child": "jQuery.sibling(a).length==1",
537 parent: "a.childNodes.length",
538 empty: "!a.childNodes.length",
539 root: "a==(a.ownerDocument||document).documentElement",
540 contains: "(a.innerText||a.innerHTML).indexOf(m[3])!=-1",
541 visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!= 'hidden')",
542 hidden: "(a.type&&a.type=='hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'",
543 enabled: "!a.disabled",
544 disabled: "a.disabled",
547 ".": "jQuery.hasWord(a,m[2])",
549 "=": "jQuery.attr(a,m[3])==m[4]",
550 "!=": "jQuery.attr(a,m[3])!=m[4]",
551 "~=": "jQuery.hasWord(jQuery.attr(a,m[3]),m[4])",
552 "|=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
553 "^=": "!jQuery.attr(a,m[3]).indexOf(m[4])",
554 "$=": "jQuery.attr(a,m[3]).substr( jQuery.attr(a,m[3]).length - m[4].length,m[4].length )==m[4]",
555 "*=": "jQuery.attr(a,m[3]).indexOf(m[4])>=0",
556 "": "m[3]=='*'?a.attributes.length>0:jQuery.attr(a,m[3])"
558 "[": "jQuery.Select(m[2],a).length"
562 "\\.\\.|/\\.\\.", "a.parentNode",
563 ">|/", "jQuery.sibling(a.firstChild)",
564 "\\+", "jQuery.sibling(a).next",
567 var s = jQuery.sibling(a);
569 for ( var i = s.n; i < s.length; i++ )
575 jQuery.Select = function( t, context ) {
576 // Make sure that the context is a DOM Element
577 if ( context && context.getElementsByTagName == undefined )
580 // Set the correct context (if none is provided)
581 context = context || jQuery.context || document;
583 if ( t.constructor != String ) return [t];
585 if ( !t.indexOf("//") ) {
586 context = context.documentElement;
587 t = t.substr(2,t.length);
588 } else if ( !t.indexOf("/") ) {
589 context = context.documentElement;
590 t = t.substr(1,t.length);
591 // FIX Assume the root element is right :(
592 if ( t.indexOf("/") >= 1 )
593 t = t.substr(t.indexOf("/"),t.length);
600 while ( t.length > 0 && last != t ) {
604 t = jQuery.cleanSpaces(t).replace( /^\/\//i, "" );
606 var foundToken = false;
608 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
609 var re = new RegExp("^(" + jQuery.token[i] + ")");
613 r = ret = jQuery.map( ret, jQuery.token[i+1] );
614 t = jQuery.cleanSpaces( t.replace( re, "" ) );
621 if ( !t.indexOf(",") || !t.indexOf("|") ) {
622 if ( ret[0] == context ) ret.shift();
623 done = jQuery.merge( done, ret );
625 t = " " + t.substr(1,t.length);
627 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
631 // Ummm, should make this work in all XML docs
632 var oid = document.getElementById(m[2]);
633 r = ret = oid ? [oid] : [];
634 t = t.replace( re2, "" );
636 if ( !m[2] || m[1] == "." ) m[2] = "*";
638 for ( var i = 0; i < ret.length; i++ )
641 jQuery.getAll(ret[i]) :
642 ret[i].getElementsByTagName(m[2])
650 var val = jQuery.filter(t,r);
652 t = jQuery.cleanSpaces(val.t);
656 if ( ret && ret[0] == context ) ret.shift();
657 done = jQuery.merge( done, ret );
662 jQuery.getAll = function(o,r) {
664 var s = o.childNodes;
665 for ( var i = 0; i < s.length; i++ )
666 if ( s[i].nodeType == 1 ) {
668 jQuery.getAll( s[i], r );
673 jQuery.attr = function(o,a,v){
674 if ( a && a.constructor == String ) {
677 "class": "className",
680 a = (fix[a] && fix[a].replace && fix[a]) || a;
681 var r = /-([a-z])/ig;
682 a = a.replace(r,function(z,b){return b.toUpperCase();});
683 if ( v != undefined ) {
685 if ( o.setAttribute && a != "disabled" )
688 return o[a] || o.getAttribute(a) || "";
693 jQuery.filter = function(t,r,not) {
696 g = function(a,f) {return jQuery.grep(a,f,true);};
698 while ( t && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
699 var re = /^\[ *@([a-z*_-][a-z0-9()_-]*) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
703 m = ["", "@", m[2], m[1], m[3]];
705 re = /^(\[) *([^\]]*) *\]/i;
709 re = /^(:)([a-z0-9*_-]*)\( *["']?([^ \)'"]*)['"]? *\)/i;
713 re = /^([:\.#]*)([a-z0-9*_-]*)/i;
718 t = t.replace( re, "" );
720 if ( m[1] == ":" && m[2] == "not" )
721 r = jQuery.filter(m[3],r,false).r;
725 if ( jQuery.g[m[1]].constructor == String )
727 else if ( jQuery.g[m[1]][m[2]] )
728 f = jQuery.g[m[1]][m[2]];
731 eval("f = function(a,i){return " + f + "}");
737 return { r: r, t: t };
740 jQuery.parents = function(a){
742 var c = a.parentNode;
743 while ( c && c != document ) {
750 jQuery.cleanSpaces = function(t){
751 return t.replace(/^\s+|\s+$/g, "");
754 jQuery.ofType = function(a,n,e) {
755 var t = jQuery.grep(jQuery.sibling(a),function(b){ return b.nodeName == a.nodeName; });
756 if ( e ) n = t.length - n - 1;
757 return n != undefined ? t[n] == a : t.length;
760 jQuery.sibling = function(a,n,e) {
762 var tmp = a.parentNode.childNodes;
763 for ( var i = 0; i < tmp.length; i++ ) {
764 if ( tmp[i].nodeType == 1 )
765 type[type.length] = tmp[i];
767 type.n = type.length - 1;
769 if ( e ) n = type.length - n - 1;
770 type.cur = ( type[n] == a );
771 type.prev = ( type.n > 0 ? type[type.n - 1] : null );
772 type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null );
776 jQuery.hasWord = function(e,a) {
777 if ( e == undefined ) return;
778 if ( e.className ) e = e.className;
779 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
782 jQuery.merge = function(a,b) {
784 for ( var k = 0; k < b.length; k++ ) d[k] = b[k];
786 for ( var i = 0; i < a.length; i++ ) {
788 for ( var j = 0; j < b.length; j++ )
791 if ( c ) d[d.length] = a[i];
797 jQuery.grep = function(a,f,s) {
798 if ( f.constructor == String )
799 f = new Function("a","i","return " + f);
802 for ( var i = 0; i < a.length; i++ )
803 if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) )
808 jQuery.map = function(a,f) {
809 if ( f.constructor == String )
810 f = new Function("a","return " + f);
813 for ( var i = 0; i < a.length; i++ ) {
815 if ( t !== null && t != undefined ) {
816 if ( t.constructor != Array ) t = [t];
817 r = jQuery.merge( t, r );
825 // Bind an event to an element
826 // Original by Dean Edwards
827 add: function(element, type, handler) {
828 // For whatever reason, IE has trouble passing the window object
829 // around, causing it to be cloned in the process
830 if ( jQuery.browser == "msie" && element.setInterval != undefined )
833 if (!handler.guid) handler.guid = jQuery.event.guid++;
834 if (!element.events) element.events = {};
835 var handlers = element.events[type];
837 handlers = element.events[type] = {};
838 if (element["on" + type])
839 handlers[0] = element["on" + type];
841 handlers[handler.guid] = handler;
842 element["on" + type] = this.handle;
844 if (!this.global[type])
845 this.global[type] = [];
846 this.global[type].push( element );
852 // Detach an event or set of events from an element
853 remove: function(element, type, handler) {
855 if (type && element.events[type])
857 delete element.events[type][handler.guid];
859 for ( var i in element.events[type] )
860 delete element.events[type][i];
862 for ( var j in element.events )
863 this.remove( element, j );
866 trigger: function(type,data,element) {
867 // Touch up the incoming data
870 // Handle a global trigger
872 var g = this.global[type];
874 for ( var i = 0; i < g.length; i++ )
875 this.trigger( type, data, g[i] );
877 // Handle triggering a single element
878 } else if ( element["on" + type] ) {
879 // Pass along a fake event
880 data.unshift( this.fix({ type: type, target: element }) );
883 element["on" + type].apply( element, data );
887 handle: function(event) {
888 // Handle adding events to items in IFrames, in IE
890 jQuery.event.fix( ((this.ownerDocument || this.document ||
891 this).parentWindow || window).event );
893 // If no correct event was found, fail
894 if ( !event ) return;
896 var returnValue = true, handlers = [];
898 for ( var j in this.events[event.type] )
899 handlers[handlers.length] = this.events[event.type][j];
901 for ( var i = 0; i < handlers.length; i++ )
902 if ( handlers[i].constructor == Function ) {
903 this.handleEvent = handlers[i];
904 if (this.handleEvent(event) === false) {
905 event.preventDefault();
906 event.stopPropagation();
913 fix: function(event) {
915 event.preventDefault = function() {
916 this.returnValue = false;
919 event.stopPropagation = function() {
920 this.cancelBubble = true;