问题
I've been having a lot of problems with jQuery 1.3.2 on only one of my sites. It's a Joomla site, so Mootools is included on the page as well (and it's too difficult to remove Mootools). Basically the problem is that calling the basic jQuery selector with one selector (eg: "a", ".myClass"
, not "html a", ".myClass td"
), will only return the first element.
I've stepped through the code and have narrowed it down to this function in the Sizzle engine:
(see for yourself, line 2058 jquery.js)
var makeArray = function(array, results) {
array = Array.prototype.slice.call( array );
if ( results ) {
results.push.apply( results, array );
return results;
}
return array;
};
I'll write it out here again with comments to show the values I've been logging after calling jQuery("a")
:
var makeArray = function(array, results) {
// "array" is an array of all the 58 links on the page
// "results" is an empty jQuery object
array = Array.prototype.slice.call( array );
// array is unchanged.
if ( results ) { // true
results.push.apply( results, array );
// "results" is now an array only holding the FIRST element.
return results;
}
return array;
};
Can someone explain this code to me? And also why it's getting rid of all but one of my elements??
回答1:
Aaargh, I finally found it. I was using an older version of the Validation plugin which wasn't compatible with jQuery 1.3+ - it had defined its own push
method which only pushed the first element into the array, and this was being called instead of the regular jQuery method.
So let this be a warning to all ye who experience weird stuff after upgrading: check the compatibility of your plugins!!
来源:https://stackoverflow.com/questions/795861/makearray-function-in-sizzle-jquery-1-3