makeArray function in Sizzle (jQuery 1.3)

好久不见. 提交于 2020-01-05 07:56:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!