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

View file

@ -77,8 +77,9 @@ 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] ) {
@ -467,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;

45
src/jquery/jquery.js vendored
View file

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

View file

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