X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=88930f227a4c6875b7eee5289617f57e90c2ea3b;hb=8937e088b6f06a0dbef36348aef5d7c10f3af340;hp=358c165469761c5603fa560b7b7393791c8c3ced;hpb=232da5dde03ae774980c9800950e60f3f4468c68;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 358c165..88930f2 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -487,7 +487,27 @@ 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++ ) { @@ -1215,28 +1235,21 @@ jQuery.extend({ }, className: { - add: function(o,c){ - if (jQuery.className.has(o,c)) return; - o.className += ( o.className ? " " : "" ) + c; + add: function( elem, c ){ + if ( jQuery.className.has( elem, c ) ) + return; + elem.className += ( elem.className ? " " : "" ) + c; }, - 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 = []; @@ -1711,7 +1740,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 @@ -1759,7 +1789,7 @@ jQuery.extend({ "\\[ *(@)S *([!*$^=]*) *('?\"?)(.*?)\\4 *\\]", // Match: [div], [div p] - "(\\[)\s*(.*?)\s*\\]", + "(\\[)\\s*(.*?)\\s*\\]", // Match: :contains('foo') "(:)S\\(\"?'?([^\\)]*?)\"?'?\\)", @@ -1785,8 +1815,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, "" ); @@ -1817,7 +1847,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 @@ -2020,12 +2050,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++; @@ -2065,7 +2099,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 @@ -2110,6 +2146,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(); @@ -2118,7 +2159,7 @@ 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; }, @@ -2602,6 +2643,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 +2656,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 +2859,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 +2872,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 +3098,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 +3188,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 +3224,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 +3295,4 @@ jQuery.macros = { } }; -jQuery.init(); +jQuery.init(); \ No newline at end of file