深入学习jquery源码之get()与reverse ()

深入学习jquery源码之get()

get([index])

概述:

取得其中一个匹配的元素。 num表示取得第几个匹配的元素。

这能够让你选择一个实际的DOM 元素并且对他直接操作,而不是通过 jQuery 函数。$(this).get(0)与$(this)[0]等价。

参数:

[index] Number

取得第 index 个位置上的元素

get()

取得所有匹配的 DOM 元素集合。

使用:

<img src="test1.jpg"/> <img src="test2.jpg"/>
$("img").get(0);
[ <img src="test1.jpg"/> ]

选择文档中所有图像作为元素数组,并用数组内建的 reverse 方法将数组反向。

<img src="test1.jpg"/> <img src="test2.jpg"/>
$("img").get().reverse();
[ <img src="test2.jpg"/> <img src="test1.jpg"/> ]

jquery源码

jQuery.fn = jQuery.prototype = {

        // The current version of jQuery being used
        jquery: version,

        constructor: jQuery,

        // Start with an empty selector
        selector: "",

        // The default length of a jQuery object is 0
        length: 0,

        toArray: function () {
            return slice.call(this);
        },

        // Get the Nth element in the matched element set OR
        // Get the whole matched element set as a clean array
        get: function (num) {
            return num != null ?

                // Return just the one element from the set
                (num < 0 ? this[num + this.length] : this[num]) :

                // Return all the elements in a clean array
                slice.call(this);
        },
        // Take an array of elements and push it onto the stack
        // (returning the new matched element set)
        pushStack: function (elems) {

            // Build a new jQuery matched element set
            var ret = jQuery.merge(this.constructor(), elems);

            // Add the old object onto the stack (as a reference)
            ret.prevObject = this;
            ret.context = this.context;

            // Return the newly-formed element set
            return ret;
        },
        slice: function () {
            return this.pushStack(slice.apply(this, arguments));
        }
        push: push,
        sort: deletedIds.sort,
        splice: deletedIds.splice
    };

猜你喜欢

转载自blog.csdn.net/qq_35029061/article/details/85226779