首页 > $('.aa', $bb) 与 $bb.find('.aa')谁的效率更高?

$('.aa', $bb) 与 $bb.find('.aa')谁的效率更高?

$('.aa', $bb) 与 $bb.find('.aa')谁的效率更高?


说到转化
那楼上的为什么不说,最终都是转化成调用jQuery.fn.find的形式呢?
认真点回答:

function (selector, context, rootjQuery) {
    var match, elem, ret, doc;

    // Handle $(""), $(null), or $(undefined)
    if (!selector) {
        return this;
    }

    // Handle $(DOMElement)
    if (selector.nodeType) {
        this.context = this[0] = selector;
        this.length = 1;
        return this;
    }

    // The body element only exists once, optimize finding it
    if (selector === "body" && !context && document.body) {
        this.context = document;
        this[0] = document.body;
        this.selector = selector;
        this.length = 1;
        return this;
    }

    // Handle HTML strings
    if (typeof selector === "string") {
        // Are we dealing with HTML string or an ID?
        if (selector.charAt(0) === "<" && selector.charAt(selector.length - 1) === ">" && selector.length >= 3) {
            // Assume that strings that start and end with <> are HTML and skip the regex check
            match = [null, selector, null];

        } else {
            match = quickExpr.exec(selector);
        }

        // Verify a match, and that no context was specified for #id
        if (match && (match[1] || !context)) {

            // HANDLE: $(html) -> $(array)
            if (match[1]) {
                context = context instanceof jQuery ? context[0] : context;
                doc = (context ? context.ownerDocument || context : document);

                // If a single string is passed in and it's a single tag
                // just do a createElement and skip the rest
                ret = rsingleTag.exec(selector);

                if (ret) {
                    if (jQuery.isPlainObject(context)) {
                        selector = [document.createElement(ret[1])];
                        jQuery.fn.attr.call(selector, context, true);

                    } else {
                        selector = [doc.createElement(ret[1])];
                    }

                } else {
                    ret = jQuery.buildFragment([match[1]], [doc]);
                    selector = (ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment).childNodes;
                }

                return jQuery.merge(this, selector);

                // HANDLE: $("#id")
            } else {
                elem = document.getElementById(match[2]);

                // Check parentNode to catch when Blackberry 4.6 returns
                // nodes that are no longer in the document #6963
                if (elem && elem.parentNode) {
                    // Handle the case where IE and Opera return items
                    // by name instead of ID
                    if (elem.id !== match[2]) {
                        return rootjQuery.find(selector);
                    }

                    // Otherwise, we inject the element directly into the jQuery object
                    this.length = 1;
                    this[0] = elem;
                }

                this.context = document;
                this.selector = selector;
                return this;
            }

            // HANDLE: $(expr, $(...))
        } else if (!context || context.jquery) {
            return (context || rootjQuery).find(selector);

            // HANDLE: $(expr, context)
            // (which is just equivalent to: $(context).find(expr)
        } else {
            return this.constructor(context).find(selector);
        }

        // HANDLE: $(function)
        // Shortcut for document ready
    } else if (jQuery.isFunction(selector)) {
        return rootjQuery.ready(selector);
    }

    if (selector.selector !== undefined) {
        this.selector = selector.selector;
        this.context = selector.context;
    }

    return jQuery.makeArray(selector, this);
}

init函数可以看出,$('.aa', $bb)最终选择的分支
return (context || rootjQuery).find(selector);
整个if-else下来肯定是要比`$bb.find('.aa')做更多的事情了,其中还有一次正则匹配。

但是$('.aa',$bb)并没有转化成$($bb).find('.aa')`。


.find()效率高,因为$('.aa', $bb)会转化成.find()


同样功能的不同写法吧

【热门文章】
【热门文章】