jquery.js

来自「自己写的一个struts+spring+hibernate测试程序」· JavaScript 代码 · 共 2,134 行 · 第 1/5 页

JS
2,134
字号
	 *	 * @name $.noConflict	 * @type undefined	 * @cat Core 	 */	noConflict: function() {		if ( jQuery._$ )			$ = jQuery._$;		return jQuery;	},	// This may seem like some crazy code, but trust me when I say that this	// is the only cross-browser way to do this. --John	isFunction: function( fn ) {		return !!fn && typeof fn != "string" && !fn.nodeName && 			fn.constructor != Array && /function/i.test( fn + "" );	},		// check if an element is in a XML document	isXMLDoc: function(elem) {		return elem.tagName && elem.ownerDocument && !elem.ownerDocument.body;	},	nodeName: function( elem, name ) {		return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();	},	/**	 * A generic iterator function, which can be used to seamlessly	 * iterate over both objects and arrays. This function is not the same	 * as $().each() - which is used to iterate, exclusively, over a jQuery	 * object. This function can be used to iterate over anything.	 *	 * The callback has two arguments:the key (objects) or index (arrays) as first	 * the first, and the value as the second.	 *	 * @example $.each( [0,1,2], function(i, n){	 *   alert( "Item #" + i + ": " + n );	 * });	 * @desc This is an example of iterating over the items in an array,	 * accessing both the current item and its index.	 *	 * @example $.each( { name: "John", lang: "JS" }, function(i, n){	 *   alert( "Name: " + i + ", Value: " + n );	 * });	 *	 * @desc This is an example of iterating over the properties in an	 * Object, accessing both the current item and its key.	 *	 * @name $.each	 * @param Object obj The object, or array, to iterate over.	 * @param Function fn The function that will be executed on every object.	 * @type Object	 * @cat JavaScript	 */	// args is for internal usage only	each: function( obj, fn, args ) {		if ( obj.length == undefined )			for ( var i in obj )				fn.apply( obj[i], args || [i, obj[i]] );		else			for ( var i = 0, ol = obj.length; i < ol; i++ )				if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break;		return obj;	},		prop: function(elem, value, type, index, prop){			// Handle executable functions			if ( jQuery.isFunction( value ) )				value = value.call( elem, [index] );							// exclude the following css properties to add px			var exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i;			// Handle passing in a number to a CSS property			return value && value.constructor == Number && type == "curCSS" && !exclude.test(prop) ?				value + "px" :				value;	},	className: {		// internal only, use addClass("class")		add: function( elem, c ){			jQuery.each( c.split(/\s+/), function(i, cur){				if ( !jQuery.className.has( elem.className, cur ) )					elem.className += ( elem.className ? " " : "" ) + cur;			});		},		// internal only, use removeClass("class")		remove: function( elem, c ){			elem.className = c != undefined ?				jQuery.grep( elem.className.split(/\s+/), function(cur){					return !jQuery.className.has( c, cur );					}).join(" ") : "";		},		// internal only, use is(".class")		has: function( t, c ) {			return jQuery.inArray( c, (t.className || t).toString().split(/\s+/) ) > -1;		}	},	/**	 * Swap in/out style options.	 * @private	 */	swap: function(e,o,f) {		for ( var i in o ) {			e.style["old"+i] = e.style[i];			e.style[i] = o[i];		}		f.apply( e, [] );		for ( var i in o )			e.style[i] = e.style["old"+i];	},	css: function(e,p) {		if ( p == "height" || p == "width" ) {			var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];			jQuery.each( d, function(){				old["padding" + this] = 0;				old["border" + this + "Width"] = 0;			});			jQuery.swap( e, old, function() {				if ( jQuery(e).is(':visible') ) {					oHeight = e.offsetHeight;					oWidth = e.offsetWidth;				} else {					e = jQuery(e.cloneNode(true))						.find(":radio").removeAttr("checked").end()						.css({							visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"						}).appendTo(e.parentNode)[0];					var parPos = jQuery.css(e.parentNode,"position") || "static";					if ( parPos == "static" )						e.parentNode.style.position = "relative";					oHeight = e.clientHeight;					oWidth = e.clientWidth;					if ( parPos == "static" )						e.parentNode.style.position = "static";					e.parentNode.removeChild(e);				}			});			return p == "height" ? oHeight : oWidth;		}		return jQuery.curCSS( e, p );	},	curCSS: function(elem, prop, force) {		var ret;		if (prop == "opacity" && jQuery.browser.msie) {			ret = jQuery.attr(elem.style, "opacity");			return ret == "" ? "1" : ret;		}				if (prop.match(/float/i))			prop = jQuery.browser.msie ? "styleFloat" : "cssFloat";		if (!force && elem.style[prop])			ret = elem.style[prop];		else if (document.defaultView && document.defaultView.getComputedStyle) {			if (prop.match(/float/i))				prop = "float";			prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase();			var cur = document.defaultView.getComputedStyle(elem, null);			if ( cur )				ret = cur.getPropertyValue(prop);			else if ( prop == "display" )				ret = "none";			else				jQuery.swap(elem, { display: "block" }, function() {				    var c = document.defaultView.getComputedStyle(this, "");				    ret = c && c.getPropertyValue(prop) || "";				});		} else if (elem.currentStyle) {			var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});			ret = elem.currentStyle[prop] || elem.currentStyle[newProp];		}		return ret;	},		clean: function(a, doc) {		var r = [];		doc = doc || document;		jQuery.each( a, function(i,arg){			if ( !arg ) return;			if ( arg.constructor == Number )				arg = arg.toString();						 // Convert html string into DOM nodes			if ( typeof arg == "string" ) {				// Trim whitespace, otherwise indexOf won't work as expected				var s = jQuery.trim(arg).toLowerCase(), div = doc.createElement("div"), tb = [];				var wrap =					 // option or optgroup					!s.indexOf("<opt") &&					[1, "<select>", "</select>"] ||										!s.indexOf("<leg") &&					[1, "<fieldset>", "</fieldset>"] ||										(!s.indexOf("<thead") || !s.indexOf("<tbody") || !s.indexOf("<tfoot") || !s.indexOf("<colg")) &&					[1, "<table>", "</table>"] ||										!s.indexOf("<tr") &&					[2, "<table><tbody>", "</tbody></table>"] ||									 	// <thead> matched above					(!s.indexOf("<td") || !s.indexOf("<th")) &&					[3, "<table><tbody><tr>", "</tr></tbody></table>"] ||										!s.indexOf("<col") &&					[2, "<table><colgroup>", "</colgroup></table>"] ||										[0,"",""];				// Go to html and back, then peel off extra wrappers				div.innerHTML = wrap[1] + arg + wrap[2];								// Move to the right depth				while ( wrap[0]-- )					div = div.firstChild;								// Remove IE's autoinserted <tbody> from table fragments				if ( jQuery.browser.msie ) {										// String was a <table>, *may* have spurious <tbody>					if ( !s.indexOf("<table") && s.indexOf("<tbody") < 0 ) 						tb = div.firstChild && div.firstChild.childNodes;											// String was a bare <thead> or <tfoot>					else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 )						tb = div.childNodes;					for ( var n = tb.length-1; n >= 0 ; --n )						if ( jQuery.nodeName(tb[n], "tbody") && !tb[n].childNodes.length )							tb[n].parentNode.removeChild(tb[n]);									}								arg = jQuery.makeArray( div.childNodes );			}			if ( 0 === arg.length && !jQuery(arg).is("form, select") )				return;			if ( arg[0] == undefined || jQuery.nodeName(arg, "form") || arg.options )				r.push( arg );			else				r = jQuery.merge( r, arg );		});		return r;	},		attr: function(elem, name, value){		var fix = jQuery.isXMLDoc(elem) ? {} : {			"for": "htmlFor",			"class": "className",			"float": jQuery.browser.msie ? "styleFloat" : "cssFloat",			cssFloat: jQuery.browser.msie ? "styleFloat" : "cssFloat",			styleFloat: jQuery.browser.msie ? "styleFloat" : "cssFloat",			innerHTML: "innerHTML",			className: "className",			value: "value",			disabled: "disabled",			checked: "checked",			readonly: "readOnly",			selected: "selected",			maxlength: "maxLength"		};				// IE actually uses filters for opacity ... elem is actually elem.style		if ( name == "opacity" && jQuery.browser.msie ) {			if ( value != undefined ) {				// IE has trouble with opacity if it does not have layout				// Force it by setting the zoom level				elem.zoom = 1; 				// Set the alpha filter to set the opacity				elem.filter = (elem.filter || "").replace(/alpha\([^)]*\)/,"") +					(parseFloat(value).toString() == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");			}			return elem.filter ? 				(parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100).toString() : "";		}				// Certain attributes only work when accessed via the old DOM 0 way		if ( fix[name] ) {			if ( value != undefined ) elem[fix[name]] = value;			return elem[fix[name]];		} else if ( value == undefined && jQuery.browser.msie && jQuery.nodeName(elem, "form") && (name == "action" || name == "method") )			return elem.getAttributeNode(name).nodeValue;		// IE elem.getAttribute passes even for style		else if ( elem.tagName ) {			if ( value != undefined ) elem.setAttribute( name, value );			if ( jQuery.browser.msie && /href|src/.test(name) && !jQuery.isXMLDoc(elem) ) 				return elem.getAttribute( name, 2 );			return elem.getAttribute( name );		// elem is actually elem.style ... set the style		} else {			name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();});			if ( value != undefined ) elem[name] = value;			return elem[name];		}	},		/**	 * Remove the whitespace from the beginning and end of a string.	 *	 * @example $.trim("  hello, how are you?  ");	 * @result "hello, how are you?"	 *	 * @name $.trim	 * @type String	 * @param String str The string to trim.	 * @cat JavaScript	 */	trim: function(t){		return t.replace(/^\s+|\s+$/g, "");	},	makeArray: function( a ) {		var r = [];		// Need to use typeof to fight Safari childNodes crashes		if ( typeof a != "array" )			for ( var i = 0, al = a.length; i < al; i++ )				r.push( a[i] );		else			r = a.slice( 0 );		return r;	},	inArray: function( b, a ) {		for ( var i = 0, al = a.length; i < al; i++ )			if ( a[i] == b )				return i;		return -1;	},	/**	 * Merge two arrays together, removing all duplicates.	 *	 * The result is the altered first argument with	 * the unique elements from the second array added.	 *	 * @example $.merge( [0,1,2], [2,3,4] )	 * @result [0,1,2,3,4]	 * @desc Merges two arrays, removing the duplicate 2	 *	 * @example var array = [3,2,1];	 * $.merge( array, [4,3,2] )	 * @result array == [3,2,1,4]	 * @desc Merges two arrays, removing the duplicates 3 and 2	 *	 * @name $.merge	 * @type Array	 * @param Array first The first array to merge, the unique elements of second added.	 * @param Array second The second array to merge into the first, unaltered.	 * @cat JavaScript	 */	merge: function(first, second) {		// We have to loop this way because IE & Opera overwrite the length		// expando of getElementsByTagName		for ( var i = 0; second[i]; i++ )			first.push(second[i]);		return first;	},	unique: function(first) {		var r = [], num = jQuery.mergeNum++;		for ( var i = 0, fl = first.length; i < fl; i++ )			if ( num != first[i].mergeNum ) {				first[i].mergeNum = num;				r.push(first[i]);			}		return r;	},	mergeNum: 0,	/**	 * Filter items out of an array, by using a filter function.	 *	 * The specified function will be passed two arguments: The	 * current array item and the index of the item in the array. The	 * function must return 'true' to keep the item in the array, 	 * false to remove it.	 *	 * @example $.grep( [0,1,2], function(i){	 *   return i > 0;	 * });	 * @result [1, 2]	 *	 * @name $.grep	 * @type Array	 * @param Array array The Array to find items in.	 * @param Function fn The function to process each item against.	 * @param Boolean inv Invert the selection - select the opposite of the function.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?