X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fevent%2Fevent.js;h=88bbbda2b31771f0daafacb28bbb33614c34b889;hb=34355cd6986d5939dc711c531263cb561cba5f24;hp=9b3788b4e695f23874462f49a9bd4e82e94697a1;hpb=c330527318775d32c87954f640c53066e7a732d9;p=jquery.git diff --git a/src/event/event.js b/src/event/event.js index 9b3788b..88bbbda 100644 --- a/src/event/event.js +++ b/src/event/event.js @@ -77,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 ](); } }, @@ -133,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); @@ -187,32 +187,32 @@ jQuery.fn.extend({ * data as the second paramter (and the handler function as the third), see * second example. * - * @example $("p").bind( "click", function() { + * @example $("p").bind("click", function(){ * alert( $(this).text() ); - * } ) + * }); * @before

Hello

* @result alert("Hello") * - * @example var handler = function(event) { + * @example function handler(event) { * alert(event.data.foo); - * }; - * $("p").bind( "click", {foo: "bar"}, handler) + * } + * $("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; } ) + * @example $("form").bind("submit", function() { return false; }) * @desc Cancel a default action and prevent it from bubbling by returning false * from your function. * - * @example $("form").bind( "submit", function(event) { + * @example $("form").bind("submit", function(event){ * event.preventDefault(); - * } ); + * }); * @desc Cancel only the default action by using the preventDefault method. * * - * @example $("form").bind( "submit", function(event) { + * @example $("form").bind("submit", function(event){ * event.stopPropagation(); - * } ) + * }); * @desc Stop only an event from bubbling by using the stopPropagation method. * * @name bind @@ -241,9 +241,9 @@ jQuery.fn.extend({ * data as the second paramter (and the handler function as the third), see * second example. * - * @example $("p").one( "click", function() { + * @example $("p").one("click", function(){ * alert( $(this).text() ); - * } ) + * }); * @before

Hello

* @result alert("Hello") * @@ -316,10 +316,6 @@ jQuery.fn.extend({ }); }, - // We're overriding the old toggle function, so - // remember it for later - _toggle: jQuery.fn.toggle, - /** * Toggle between two function calls every other click. * Whenever a matched element is clicked, the first specified function @@ -341,9 +337,10 @@ jQuery.fn.extend({ * @cat Events */ toggle: function() { - // save reference to arguments for access in closure + // Save reference to arguments for access in closure var a = arguments; - return typeof a[0] == "function" && typeof a[1] == "function" ? this.click(function(e) { + + return this.click(function(e) { // Figure out which function to execute this.lastToggle = this.lastToggle == 0 ? 1 : 0; @@ -352,10 +349,7 @@ jQuery.fn.extend({ // and execute the function return a[this.lastToggle].apply( this, [e] ) || false; - }) : - - // Otherwise, execute the old toggle function - this._toggle.apply( this, arguments ); + }); }, /** @@ -414,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. @@ -423,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; @@ -461,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; @@ -486,7 +494,7 @@ new function(){ * @name scroll * @type jQuery * @param Function fn A function to bind to the scroll event on each of the matched elements. - * @cat Events/Browser + * @cat Events */ /** @@ -501,7 +509,7 @@ new function(){ * @name submit * @type jQuery * @param Function fn A function to bind to the submit event on each of the matched elements. - * @cat Events/Form + * @cat Events */ /** @@ -516,7 +524,7 @@ new function(){ * * @name submit * @type jQuery - * @cat Events/Form + * @cat Events */ /** @@ -529,7 +537,7 @@ new function(){ * @name focus * @type jQuery * @param Function fn A function to bind to the focus event on each of the matched elements. - * @cat Events/UI + * @cat Events */ /** @@ -545,7 +553,7 @@ new function(){ * * @name focus * @type jQuery - * @cat Events/UI + * @cat Events */ /** @@ -558,7 +566,7 @@ new function(){ * @name keydown * @type jQuery * @param Function fn A function to bind to the keydown event on each of the matched elements. - * @cat Events/Keyboard + * @cat Events */ /** @@ -571,7 +579,7 @@ new function(){ * @name dblclick * @type jQuery * @param Function fn A function to bind to the dblclick event on each of the matched elements. - * @cat Events/Mouse + * @cat Events */ /** @@ -584,7 +592,7 @@ new function(){ * @name keypress * @type jQuery * @param Function fn A function to bind to the keypress event on each of the matched elements. - * @cat Events/Keyboard + * @cat Events */ /** @@ -597,7 +605,7 @@ new function(){ * @name error * @type jQuery * @param Function fn A function to bind to the error event on each of the matched elements. - * @cat Events/Browser + * @cat Events */ /** @@ -610,7 +618,7 @@ new function(){ * @name blur * @type jQuery * @param Function fn A function to bind to the blur event on each of the matched elements. - * @cat Events/UI + * @cat Events */ /** @@ -626,7 +634,7 @@ new function(){ * * @name blur * @type jQuery - * @cat Events/UI + * @cat Events */ /** @@ -639,7 +647,7 @@ new function(){ * @name load * @type jQuery * @param Function fn A function to bind to the load event on each of the matched elements. - * @cat Events/Browser + * @cat Events */ /** @@ -652,7 +660,7 @@ new function(){ * @name select * @type jQuery * @param Function fn A function to bind to the select event on each of the matched elements. - * @cat Events/Form + * @cat Events */ /** @@ -665,7 +673,7 @@ new function(){ * * @name select * @type jQuery - * @cat Events/Form + * @cat Events */ /** @@ -678,7 +686,7 @@ new function(){ * @name mouseup * @type jQuery * @param Function fn A function to bind to the mouseup event on each of the matched elements. - * @cat Events/Mouse + * @cat Events */ /** @@ -691,7 +699,7 @@ new function(){ * @name unload * @type jQuery * @param Function fn A function to bind to the unload event on each of the matched elements. - * @cat Events/Browser + * @cat Events */ /** @@ -704,7 +712,7 @@ new function(){ * @name change * @type jQuery * @param Function fn A function to bind to the change event on each of the matched elements. - * @cat Events/Form + * @cat Events */ /** @@ -717,7 +725,7 @@ new function(){ * @name mouseout * @type jQuery * @param Function fn A function to bind to the mouseout event on each of the matched elements. - * @cat Events/Mouse + * @cat Events */ /** @@ -730,7 +738,7 @@ new function(){ * @name keyup * @type jQuery * @param Function fn A function to bind to the keyup event on each of the matched elements. - * @cat Events/Keyboard + * @cat Events */ /** @@ -743,7 +751,7 @@ new function(){ * @name click * @type jQuery * @param Function fn A function to bind to the click event on each of the matched elements. - * @cat Events/Mouse + * @cat Events */ /** @@ -756,7 +764,7 @@ new function(){ * * @name click * @type jQuery - * @cat Events/Mouse + * @cat Events */ /** @@ -769,7 +777,7 @@ new function(){ * @name resize * @type jQuery * @param Function fn A function to bind to the resize event on each of the matched elements. - * @cat Events/Browser + * @cat Events */ /** @@ -782,7 +790,7 @@ new function(){ * @name mousemove * @type jQuery * @param Function fn A function to bind to the mousemove event on each of the matched elements. - * @cat Events/Mouse + * @cat Events */ /** @@ -795,7 +803,7 @@ new function(){ * @name mousedown * @type jQuery * @param Function fn A function to bind to the mousedown event on each of the matched elements. - * @cat Events/Mouse + * @cat Events */ /** @@ -808,7 +816,7 @@ new function(){ * @name mouseover * @type jQuery * @param Function fn A function to bind to the mousedown event on each of the matched elements. - * @cat Events/Mouse + * @cat Events */ jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," + "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," + @@ -881,4 +889,4 @@ if (jQuery.browser.msie) jQuery.event.remove(els[i-1], type); while (--i); } - }); \ No newline at end of file + });