X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=7b712c955583cb26c0c0d0a72f96eb63ac155257;hb=65a88fae8919e5da774dcc45c9b42220f875b697;hp=af729695b92db3245847fd53de115d1e2b7447aa;hpb=cc5f46454fbf34ae8a8e526a621cab55e1c79cdb;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index af72969..7b712c9 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -162,6 +162,8 @@ var $ = jQuery; * technically, chainable - there really isn't much use for chaining against it. * You can have as many $(document).ready events on your page as you like. * + * See ready(Function) for details about the ready event. + * * @example $(function(){ * // Document is ready * }); @@ -485,12 +487,32 @@ jQuery.fn = jQuery.prototype = { * @type String * @cat DOM */ + + /** + * Set the text contents of all matched elements. This has the same + * effect as calling .html() with your specified string. + * + * @example $("p").text("Some new text."); + * @before

Test Paragraph.

+ * @result

Some new text.

+ * + * @param String val The text value to set the contents of the element to. + * + * @name text + * @type String + * @cat DOM + */ text: function(e) { + // A surprisingly high number of people expect the + // .text() method to do this, so lets do it! + if ( typeof e == "string" ) + return this.html( e ); + e = e || this; var t = ""; - for ( var j = 0; j < e.length; j++ ) { + for ( var j = 0, el = e.length; j < el; j++ ) { var r = e[j].childNodes; - for ( var i = 0; i < r.length; i++ ) + for ( var i = 0, rl = r.length; i < rl; i++ ) if ( r[i].nodeType != 8 ) t += r[i].nodeType != 1 ? r[i].nodeValue : jQuery.fn.text([ r[i] ]); @@ -860,7 +882,7 @@ jQuery.fn = jQuery.prototype = { return this.pushStack( t.constructor == Array && jQuery.map(this,function(a){ - for ( var i = 0; i < t.length; i++ ) + for ( var i = 0, tl = t.length; i < tl; i++ ) if ( jQuery.filter(t[i],[a]).r.length ) return a; return null; @@ -1008,7 +1030,7 @@ jQuery.fn = jQuery.prototype = { if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" ) obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody")); - for ( var i=0; i < a.length; i++ ) + for ( var i = 0, al = a.length; i < al; i++ ) fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] ); }); @@ -1142,7 +1164,7 @@ jQuery.extend({ jQuery.fn[ i ] = function(){ var a = arguments; return this.each(function(){ - for ( var j = 0; j < a.length; j++ ) + for ( var j = 0, al = a.length; j < al; j++ ) jQuery(a[j])[n]( this ); }); }; @@ -1207,34 +1229,26 @@ jQuery.extend({ for ( var i in obj ) fn.apply( obj[i], args || [i, obj[i]] ); else - for ( var i = 0; i < obj.length; i++ ) + for ( var i = 0, ol = obj.length; i < ol; i++ ) if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break; return obj; }, className: { - add: function(o,c){ - if (jQuery.className.has(o,c)) return; - o.className += ( o.className ? " " : "" ) + c; + add: function( elem, c ){ + jQuery.each( c.split(/\s+/), function(i, cur){ + if ( !jQuery.className.has( elem.className, cur ) ) + elem.className += ( elem.className ? " " : "" ) + cur; + }); }, - remove: function(o,c){ - if( !c ) { - o.className = ""; - } else { - var classes = o.className.split(" "); - for(var i=0; i=0", - "": "z" + "": "z", + _resort: function(m){ + return ["", m[1], m[3], m[2], m[5]]; + }, + _prefix: "z=jQuery.attr(a,m[3]);" }, "[": "jQuery.find(m[2],a).length" }, - /** + /** * All elements on a specified axis. * * @private * @name $.sibling * @type Array * @param Element elem The element to find all the siblings of (including itself). - * @cat DOM/Traversing - */ + * @cat DOM/Traversing + */ sibling: function( n, elem ) { var r = []; @@ -1537,7 +1567,7 @@ jQuery.extend({ if ( m ) { // Perform our own iteration and filter - for ( var i = 0; i < ret.length; i++ ) + for ( var i = 0, rl = ret.length; i < rl; i++ ) for ( var c = ret[i].firstChild; c; c = c.nextSibling ) if ( c.nodeType == 1 && ( c.nodeName == m[1].toUpperCase() || m[1] == "*" ) ) r.push( c ); @@ -1623,7 +1653,7 @@ jQuery.extend({ // We need to find all descendant elements, it is more // efficient to use getAll() when we are already further down // the tree - we try to recognize that here - for ( var i = 0; i < ret.length; i++ ) + for ( var i = 0, rl = ret.length; i < rl; i++ ) jQuery.merge( r, m[1] != "" && ret.length != 1 ? jQuery.getAll( ret[i], [], m[1], m[2], rec ) : @@ -1643,7 +1673,7 @@ jQuery.extend({ r = []; // Then try to find the element with the ID - for ( var i = 0; i < tmp.length; i++ ) + for ( var i = 0, tl = tmp.length; i < tl; i++ ) if ( tmp[i].getAttribute("id") == m[2] ) { r = [ tmp[i] ]; break; @@ -1709,7 +1739,8 @@ jQuery.extend({ value: "value", disabled: "disabled", checked: "checked", - readonly: "readOnly" + readonly: "readOnly", + selected: "selected" }; // IE actually uses filters for opacity ... elem is actually elem.style @@ -1757,7 +1788,7 @@ jQuery.extend({ "\\[ *(@)S *([!*$^=]*) *('?\"?)(.*?)\\4 *\\]", // Match: [div], [div p] - "(\\[)\s*(.*?)\s*\\]", + "(\\[)\\s*(.*?)\\s*\\]", // Match: :contains('foo') "(:)S\\(\"?'?([^\\)]*?)\"?'?\\)", @@ -1772,7 +1803,7 @@ jQuery.extend({ var p = jQuery.parse; - for ( var i = 0; i < p.length; i++ ) { + for ( var i = 0, pl = p.length; i < pl; i++ ) { // Look for, and replace, string-like sequences // and finally build a regexp out of it @@ -1783,8 +1814,8 @@ jQuery.extend({ if ( m ) { // Re-organize the first match - if ( !i ) - m = ["",m[1], m[3], m[2], m[5]]; + if ( jQuery.expr[ m[1] ]._resort ) + m = jQuery.expr[ m[1] ]._resort( m ); // Remove what we just matched t = t.replace( re, "" ); @@ -1815,7 +1846,7 @@ jQuery.extend({ // Build a custom macro to enclose it eval("f = function(a,i){" + - ( m[1] == "@" ? "z=jQuery.attr(a,m[3]);" : "" ) + + ( jQuery.expr[ m[1] ]._prefix || "" ) + "return " + f + "}"); // Execute it against the current filter @@ -1866,7 +1897,7 @@ jQuery.extend({ var r = []; if ( a.constructor != Array ) { - for ( var i = 0; i < a.length; i++ ) + for ( var i = 0, al = a.length; i < al; i++ ) r.push( a[i] ); } else r = a.slice( 0 ); @@ -1875,7 +1906,7 @@ jQuery.extend({ }, inArray: function( b, a ) { - for ( var i = 0; i < a.length; i++ ) + for ( var i = 0, al = a.length; i < al; i++ ) if ( a[i] == b ) return i; return -1; @@ -1903,7 +1934,7 @@ jQuery.extend({ // Now check for duplicates between the two arrays // and only add the unique items - for ( var i = 0; i < second.length; i++ ) { + for ( var i = 0, sl = second.length; i < sl; i++ ) { // Check for duplicates if ( jQuery.inArray( second[i], r ) == -1 ) // The item is unique, add it @@ -1942,7 +1973,7 @@ jQuery.extend({ // Go through the array, only saving the items // that pass the validator function - for ( var i = 0; i < elems.length; i++ ) + for ( var i = 0, el = elems.length; i < el; i++ ) if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) ) result.push( elems[i] ); @@ -1988,7 +2019,7 @@ jQuery.extend({ // Go through the array, translating each of the items to their // new value (or values). - for ( var i = 0; i < elems.length; i++ ) { + for ( var i = 0, el = elems.length; i < el; i++ ) { var val = fn(elems[i],i); if ( val !== null && val != undefined ) { @@ -1999,7 +2030,7 @@ jQuery.extend({ var r = [ result[0] ]; - check: for ( var i = 1; i < result.length; i++ ) { + check: for ( var i = 1, rl = result.length; i < rl; i++ ) { for ( var j = 0; j < i; j++ ) if ( result[i] == r[j] ) continue check; @@ -2018,12 +2049,16 @@ jQuery.extend({ // Bind an event to an element // Original by Dean Edwards - add: function(element, type, handler) { + add: function(element, type, handler, data) { // For whatever reason, IE has trouble passing the window object // around, causing it to be cloned in the process if ( jQuery.browser.msie && element.setInterval != undefined ) element = window; + // if data is passed, bind to handler + if( data ) + handler.data = data; + // Make sure that the function being executed has a unique ID if ( !handler.guid ) handler.guid = this.guid++; @@ -2063,7 +2098,9 @@ jQuery.extend({ // Detach an event or set of events from an element remove: function(element, type, handler) { if (element.events) - if (type && element.events[type]) + if ( type && type.type ) + delete element.events[ type.type ][ type.handler.guid ]; + else if (type && element.events[type]) if ( handler ) delete element.events[type][handler.guid]; else @@ -2082,7 +2119,7 @@ jQuery.extend({ if ( !element ) { var g = this.global[type]; if ( g ) - for ( var i = 0; i < g.length; i++ ) + for ( var i = 0, gl = g.length; i < gl; i++ ) this.trigger( type, data, g[i] ); // Handle triggering a single element @@ -2100,9 +2137,6 @@ jQuery.extend({ event = jQuery.event.fix( event || window.event || {} ); // Empty object is for triggered events with no data - // If no correct event was found, fail - if ( !event ) return false; - var returnValue = true; var c = this.events[event.type]; @@ -2111,6 +2145,11 @@ jQuery.extend({ args.unshift( event ); for ( var j in c ) { + // Pass in a reference to the handler function itself + // So that we can later remove it + args[0].handler = c[j]; + args[0].data = c[j].data; + if ( c[j].apply( this, args ) === false ) { event.preventDefault(); event.stopPropagation(); @@ -2119,26 +2158,25 @@ jQuery.extend({ } // Clean up added properties in IE to prevent memory leak - if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = null; + if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = event.handler = event.data = null; return returnValue; }, fix: function(event) { - // check IE - if(jQuery.browser.msie) { - // fix target property, if available - // check prevents overwriting of fake target coming from trigger - if(event.srcElement) - event.target = event.srcElement; - - // calculate pageX/Y + // Fix target property, if necessary + if ( !event.target && event.srcElement ) + event.target = event.srcElement; + + // Calculate pageX/Y if missing and clientX/Y available + if ( typeof event.pageX == "undefined" && typeof event.clientX != "undefined" ) { var e = document.documentElement, b = document.body; event.pageX = event.clientX + (e.scrollLeft || b.scrollLeft); event.pageY = event.clientY + (e.scrollTop || b.scrollTop); + } - // check safari and if target is a textnode - } else if(jQuery.browser.safari && event.target.nodeType == 3) { + // Check safari and if target is a textnode + if ( jQuery.browser.safari && event.target.nodeType == 3 ) { // target is readonly, clone the event object event = jQuery.extend({}, event); // get parentnode from textnode @@ -2146,15 +2184,17 @@ jQuery.extend({ } // fix preventDefault and stopPropagation - if (!event.preventDefault) + if (!event.preventDefault) { event.preventDefault = function() { this.returnValue = false; }; + } - if (!event.stopPropagation) + if (!event.stopPropagation) { event.stopPropagation = function() { this.cancelBubble = true; }; + } return event; } @@ -2602,6 +2642,7 @@ jQuery.macros = { /** * Get the html contents of the first matched element. + * This property is not available on XML documents. * * @example $("div").html(); * @before
@@ -2614,6 +2655,7 @@ jQuery.macros = { /** * Set the html contents of every matched element. + * This property is not available on XML documents. * * @example $("div").html("new stuff"); * @before
@@ -2816,35 +2858,7 @@ jQuery.macros = { * Get a set of elements containing the unique ancestors of the matched * set of elements (except for the root element). * - * @example $("span").ancestors() - * @before

Hello

Hello Again
- * @result [ ...,
...
,

Hello

] - * - * @name ancestors - * @type jQuery - * @cat DOM/Traversing - */ - - /** - * Get a set of elements containing the unique ancestors of the matched - * set of elements, and filtered by an expression. - * - * @example $("span").ancestors("p") - * @before

Hello

Hello Again
- * @result [

Hello

] - * - * @name ancestors - * @type jQuery - * @param String expr An expression to filter the ancestors with - * @cat DOM/Traversing - */ - ancestors: jQuery.parents, - - /** - * Get a set of elements containing the unique ancestors of the matched - * set of elements (except for the root element). - * - * @example $("span").ancestors() + * @example $("span").parents() * @before

Hello

Hello Again
* @result [ ...,
...
,

Hello

] * @@ -2857,7 +2871,7 @@ jQuery.macros = { * Get a set of elements containing the unique ancestors of the matched * set of elements, and filtered by an expression. * - * @example $("span").ancestors("p") + * @example $("span").parents("p") * @before

Hello

Hello Again
* @result [

Hello

] * @@ -3083,15 +3097,19 @@ jQuery.macros = { }, /** - * Removes the specified class from the set of matched elements. + * Removes all or the specified class from the set of matched elements. * - * @example $("p").removeClass("selected") + * @example $("p").removeClass() * @before

Hello

* @result [

Hello

] * + * @example $("p").removeClass("selected") + * @before

Hello

+ * @result [

Hello

] + * * @name removeClass * @type jQuery - * @param String class A CSS class to remove from the elements + * @param String class (optional) A CSS class to remove from the elements * @cat DOM */ removeClass: function(c){ @@ -3169,12 +3187,24 @@ jQuery.macros = { * default behaviour. To stop both default action and event bubbling, your handler * has to return false. * + * In most cases, you can define your event handlers as anonymous functions + * (see first example). In cases where that is not possible, you can pass additional + * data as the second paramter (and the handler function as the third), see + * second example. + * * @example $("p").bind( "click", function() { * alert( $(this).text() ); * } ) * @before

Hello

* @result alert("Hello") * + * @example var handler = function(event) { + * alert(event.data.foo); + * }; + * $("p").bind( "click", {foo: "bar"}, handler) + * @result alert("bar") + * @desc Pass some additional data to the event handler. + * * @example $("form").bind( "submit", function() { return false; } ) * @desc Cancel a default action and prevent it from bubbling by returning false * from your function. @@ -3193,11 +3223,12 @@ jQuery.macros = { * @name bind * @type jQuery * @param String type An event type + * @param Object data (optional) Additional data passed to the event handler as event.data * @param Function fn A function to bind to the event on each of the set of matched elements * @cat Events */ - bind: function( type, fn ) { - jQuery.event.add( this, type, fn ); + bind: function( type, data, fn ) { + jQuery.event.add( this, type, fn || data, data ); }, /** @@ -3263,4 +3294,4 @@ jQuery.macros = { } }; -jQuery.init(); +jQuery.init(); \ No newline at end of file