X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;ds=sidebyside;f=src%2Fevent%2Fevent.js;h=88bbbda2b31771f0daafacb28bbb33614c34b889;hb=34355cd6986d5939dc711c531263cb561cba5f24;hp=a4cba96cca4e44a8f593d197ee3acf50d69a24ff;hpb=41cc839a2dff6342aaba048eae36d66d2a25d288;p=jquery.git diff --git a/src/event/event.js b/src/event/event.js index a4cba96..88bbbda 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -1,100 +1,3 @@ -/** - * Describes the nature of the event. - * - * Native by all browsers. - * - * @example $("a").click(function(event) { - * alert(event.type); - * }); - * @result "click" - * - * @property - * @name event.type - * @type String - * @cat Events - */ - -/** - * Contains the DOM element that issued the event. This can be - * the element that registered for the event or a child of it. - * - * Fixed where necessary (IE, Safari). - * - * Use to implement event delegation. - * - * @example $("a[@href=http://google.com]").click(function(event) { - * alert(event.target.href); - * }); - * @result "http://google.com" - * @desc Alerts the href property of the target element. - * - * @example function handler(event) { - * var target = $(event.target); - * if( target.is("li") ) { - * target.children().toggle(); - * } - * } - * $("ul").click(handler).find("li > ").hide(); - * @desc Implements a simple event delegation: The click handler is added - * to an unordered list, and the children of it's li children are hidden. - * Clicking one of the li children toggles (see toggle()) their children. - * - * @property - * @name event.target - * @type Element - * @cat Events - */ - -/** - * The pageX/Y property pair returns the mouse coordinates relative to the document. - * - * Fixed where necessary (IE). - * - * @example $("a").click(function(event) { - * alert("Current mouse position: " + event.pageX + ", " + event.pageY ); - * }); - * @result "Current mouse position: 130, 640" - * - * @property - * @name event.pageX/Y - * @type String - * @cat Events - */ - -/** - * Stops the bubbling of an event, prohibiting other handlers to get to - * handle that event. - * - * Fixed where necessary (IE). - * - * @example $("p").click(function(event){ - * event.stopPropagation(); - * // do something - * }); - * @desc Prohibits other event handlers to be called. - * - * @name event.stopPropagation - * @type undefined - * @cat Events - */ - -/** - * Prevents the browser from executing the default action. - * - * Fixed where necessary (IE). - * - * @example $("a").click(function(event){ - * event.preventDefault(); - * // do something - * }); - * @desc Stops the browser from changing the page to the - * href of any anchors. - * - * @name event.preventDefault - * @type undefined - * @cat Events - */ - /* * A number of helper functions used for managing events. * Many of the ideas behind this code orignated from @@ -174,20 +77,20 @@ jQuery.event = { if ( !element ) { var g = this.global[type]; if ( g ) - for ( var i = 0, gl = g.length; i < gl; i++ ) - this.trigger( type, data, g[i] ); + jQuery.each( g, function(){ + jQuery.event.trigger( type, data, this ); + }); // Handle triggering a single element } else if ( element["on" + type] ) { - if ( element[ type ] && element[ type ].constructor == Function ) - element[ type ](); - else { - // Pass along a fake event - data.unshift( this.fix({ type: type, target: element }) ); + // Pass along a fake event + data.unshift( this.fix({ type: type, target: element }) ); - // Trigger the event - element["on" + type].apply( element, data ); - } + // Trigger the event + var val = element["on" + type].apply( element, data ); + + if ( val !== false && jQuery.isFunction( element[ type ] ) ) + element[ type ](); } }, @@ -230,7 +133,7 @@ jQuery.event = { event.target = event.srcElement; // Calculate pageX/Y if missing and clientX/Y available - if ( typeof event.pageX == "undefined" && typeof event.clientX != "undefined" ) { + if ( event.pageX == undefined && 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); @@ -505,6 +408,10 @@ jQuery.fn.extend({ * and attaching a function to that. By using this method, your bound Function * will be called the instant the DOM is ready to be read and manipulated, * which is exactly what 99.99% of all Javascript code needs to run. + * + * There is one argument passed to the ready event handler: A reference to + * the jQuery function. You can name that argument whatever you like, and + * can therefore stick with the $ alias without risc of naming collisions. * * Please ensure you have no code in your <body> onload event handler, * otherwise $(document).ready() may not fire. @@ -514,21 +421,30 @@ jQuery.fn.extend({ * * @example $(document).ready(function(){ Your code here... }); * + * @example jQuery(function($) { + * // Your code using failsafe $ alias here... + * }); + * @desc Uses both the shortcut for $(document).ready() and the argument + * to write failsafe jQuery code using the $ alias, without relying on the + * global alias. + * * @name ready * @type jQuery * @param Function fn The function to be executed when the DOM is ready. * @cat Events + * @see $.noConflict() + * @see $(Function) */ ready: function(f) { // If the DOM is already ready if ( jQuery.isReady ) // Execute the function immediately - f.apply( document ); + f.apply( document, [jQuery] ); // Otherwise, remember the function for later else { // Add the function to the wait list - jQuery.readyList.push( f ); + jQuery.readyList.push( function() { return f.apply(this, [jQuery]) } ); } return this; @@ -552,8 +468,9 @@ jQuery.extend({ // If there are functions bound, to execute if ( jQuery.readyList ) { // Execute all of them - for ( var i = 0; i < jQuery.readyList.length; i++ ) - jQuery.readyList[i].apply( document ); + jQuery.each( jQuery.readyList, function(){ + this.apply( document ); + }); // Reset the list of functions jQuery.readyList = null;