Converted a lot of for loops to use jQuery.each() instead.

This commit is contained in:
John Resig 2007-01-14 21:49:59 +00:00
parent 866187fff6
commit 34355cd698
4 changed files with 51 additions and 49 deletions

View file

@ -789,8 +789,9 @@ jQuery.extend({
// of form elements // of form elements
if ( a.constructor == Array || a.jquery ) if ( a.constructor == Array || a.jquery )
// Serialize the form elements // Serialize the form elements
for ( var i = 0; i < a.length; i++ ) jQuery.each( a, function(){
s.push( encodeURIComponent(a[i].name) + "=" + encodeURIComponent( a[i].value ) ); s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
});
// Otherwise, assume that it's an object of key/value pairs // Otherwise, assume that it's an object of key/value pairs
else else
@ -798,8 +799,9 @@ jQuery.extend({
for ( var j in a ) for ( var j in a )
// If the value is an array then the key names need to be repeated // If the value is an array then the key names need to be repeated
if ( a[j].constructor == Array ) if ( a[j].constructor == Array )
for ( var k = 0; k < a[j].length; k++ ) jQuery.each( a[j], function(){
s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j][k] ) ); s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
});
else else
s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) ); s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );

View file

@ -77,8 +77,9 @@ jQuery.event = {
if ( !element ) { if ( !element ) {
var g = this.global[type]; var g = this.global[type];
if ( g ) if ( g )
for ( var i = 0, gl = g.length; i < gl; i++ ) jQuery.each( g, function(){
this.trigger( type, data, g[i] ); jQuery.event.trigger( type, data, this );
});
// Handle triggering a single element // Handle triggering a single element
} else if ( element["on" + type] ) { } else if ( element["on" + type] ) {
@ -467,8 +468,9 @@ jQuery.extend({
// If there are functions bound, to execute // If there are functions bound, to execute
if ( jQuery.readyList ) { if ( jQuery.readyList ) {
// Execute all of them // Execute all of them
for ( var i = 0; i < jQuery.readyList.length; i++ ) jQuery.each( jQuery.readyList, function(){
jQuery.readyList[i].apply( document ); this.apply( document );
});
// Reset the list of functions // Reset the list of functions
jQuery.readyList = null; jQuery.readyList = null;

45
src/jquery/jquery.js vendored
View file

@ -546,18 +546,16 @@ jQuery.fn = jQuery.prototype = {
text: function(e) { text: function(e) {
if ( typeof e == "string" ) if ( typeof e == "string" )
return this.empty().append( document.createTextNode( e ) ); return this.empty().append( document.createTextNode( e ) );
else {
e = e || this; var t = "";
var t = ""; jQuery.each( e || this, function(){
for ( var j = 0, el = e.length; j < el; j++ ) { jQuery.each( this.childNodes, function(){
var r = e[j].childNodes; if ( this.nodeType != 8 )
for ( var i = 0, rl = r.length; i < rl; i++ ) t += this.nodeType != 1 ?
if ( r[i].nodeType != 8 ) this.nodeValue : jQuery.fn.text([ this ]);
t += r[i].nodeType != 1 ? });
r[i].nodeValue : jQuery.fn.text([ r[i] ]); });
} return t;
return t;
}
}, },
/** /**
@ -1102,8 +1100,9 @@ jQuery.fn = jQuery.prototype = {
if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" ) if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" )
obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody")); obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody"));
for ( var i = 0, al = a.length; i < al; i++ ) jQuery.each( a, function(){
fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] ); fn.apply( obj, [ clone ? this.cloneNode(true) : this ] );
});
}); });
} }
@ -1316,10 +1315,10 @@ jQuery.extend({
if ( p == "height" || p == "width" ) { if ( p == "height" || p == "width" ) {
var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"]; var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
for ( var i = 0, dl = d.length; i < dl; i++ ) { jQuery.each( d, function(){
old["padding" + d[i]] = 0; old["padding" + this] = 0;
old["border" + d[i] + "Width"] = 0; old["border" + this + "Width"] = 0;
} });
jQuery.swap( e, old, function() { jQuery.swap( e, old, function() {
if (jQuery.css(e,"display") != "none") { if (jQuery.css(e,"display") != "none") {
@ -1395,10 +1394,8 @@ jQuery.extend({
clean: function(a) { clean: function(a) {
var r = []; var r = [];
for ( var i = 0, al = a.length; i < al; i++ ) { jQuery.each( a, function(i,arg){
var arg = a[i]; if ( !arg ) return;
if ( !arg ) continue;
if ( arg.constructor == Number ) if ( arg.constructor == Number )
arg = arg.toString(); arg = arg.toString();
@ -1453,14 +1450,14 @@ jQuery.extend({
} }
if ( arg.length === 0 ) if ( arg.length === 0 )
continue; return;
if ( arg[0] == undefined ) if ( arg[0] == undefined )
r.push( arg ); r.push( arg );
else else
r = jQuery.merge( r, arg ); r = jQuery.merge( r, arg );
} });
return r; return r;
}, },

View file

@ -153,10 +153,11 @@ jQuery.extend({
if ( m ) { if ( m ) {
// Perform our own iteration and filter // Perform our own iteration and filter
for ( var i = 0, rl = ret.length; i < rl; i++ ) jQuery.each( ret, function(){
for ( var c = ret[i].firstChild; c; c = c.nextSibling ) for ( var c = this.firstChild; c; c = c.nextSibling )
if ( c.nodeType == 1 && ( c.nodeName == m[1].toUpperCase() || m[1] == "*" ) ) if ( c.nodeType == 1 && ( c.nodeName == m[1].toUpperCase() || m[1] == "*" ) )
r.push( c ); r.push( c );
});
ret = r; ret = r;
t = jQuery.trim( t.replace( re, "" ) ); t = jQuery.trim( t.replace( re, "" ) );
@ -235,20 +236,20 @@ jQuery.extend({
// We need to find all descendant elements, it is more // We need to find all descendant elements, it is more
// efficient to use getAll() when we are already further down // efficient to use getAll() when we are already further down
// the tree - we try to recognize that here // the tree - we try to recognize that here
for ( var i = 0, rl = ret.length; i < rl; i++ ) { jQuery.each( ret, function(){
// Grab the tag name being searched for // Grab the tag name being searched for
var tag = m[1] != "" || m[0] == "" ? "*" : m[2]; var tag = m[1] != "" || m[0] == "" ? "*" : m[2];
// Handle IE7 being really dumb about <object>s // Handle IE7 being really dumb about <object>s
if ( ret[i].nodeName.toUpperCase() == "OBJECT" && tag == "*" ) if ( this.nodeName.toUpperCase() == "OBJECT" && tag == "*" )
tag = "param"; tag = "param";
jQuery.merge( r, jQuery.merge( r,
m[1] != "" && ret.length != 1 ? m[1] != "" && ret.length != 1 ?
jQuery.getAll( ret[i], [], m[1], m[2], rec ) : jQuery.getAll( this, [], m[1], m[2], rec ) :
ret[i].getElementsByTagName( tag ) this.getElementsByTagName( tag )
); );
} });
// It's faster to filter by class and be done with it // It's faster to filter by class and be done with it
if ( m[1] == "." && ret.length == 1 ) if ( m[1] == "." && ret.length == 1 )
@ -263,11 +264,12 @@ jQuery.extend({
r = []; r = [];
// Then try to find the element with the ID // Then try to find the element with the ID
for ( var i = 0, tl = tmp.length; i < tl; i++ ) jQuery.each( tmp, function(){
if ( tmp[i].getAttribute("id") == m[2] ) { if ( this.getAttribute("id") == m[2] ) {
r = [ tmp[i] ]; r = [ this ];
break; return false;
} }
});
} }
ret = r; ret = r;
@ -300,14 +302,13 @@ jQuery.extend({
// Look for common filter expressions // Look for common filter expressions
while ( t && /^[a-z[({<*:.#]/i.test(t) ) { while ( t && /^[a-z[({<*:.#]/i.test(t) ) {
var p = jQuery.parse; var p = jQuery.parse, m;
for ( var i = 0, pl = p.length; i < pl; i++ ) { jQuery.each( p, function(i,re){
// Look for, and replace, string-like sequences // Look for, and replace, string-like sequences
// and finally build a regexp out of it // and finally build a regexp out of it
var re = p[i]; m = re.exec( t );
var m = re.exec( t );
if ( m ) { if ( m ) {
// Remove what we just matched // Remove what we just matched
@ -317,9 +318,9 @@ jQuery.extend({
if ( jQuery.expr[ m[1] ]._resort ) if ( jQuery.expr[ m[1] ]._resort )
m = jQuery.expr[ m[1] ]._resort( m ); m = jQuery.expr[ m[1] ]._resort( m );
break; return false;
} }
} });
// :not() is a special case that can be optimized by // :not() is a special case that can be optimized by
// keeping it out of the expression list // keeping it out of the expression list