What is a jQuery Object?

后端 未结 2 1180
逝去的感伤
逝去的感伤 2020-12-02 19:17

JavaScript kind of redefines what an Array means because an array is an object with a .length property and methods like .slice() and .join()<

2条回答
  •  佛祖请我去吃肉
    2020-12-02 19:55

    A jQuery object is array-like which means that it contains zero or more indexes (properties which names are positive integers starting with zero). Besides those indexes, a jQuery object contains these properties:

    • length
    • context
    • selector

    And also around 140 inherited methods (which are defined on the jQuery.prototype object - you can do console.dir(jQuery.prototype) to get a full list... ).

    Note that jQuery objects do not contain (or inherit) the Array methods (slice, substr, ...). If you want to execute those methods on your jQuery object, use call/apply.


    For example, if you have 3 TEXTAREA elements on the page and you do this:

    var j = $('textarea');
    

    then this j jQuery object will contain these properties:

    • 0 - reference to the first TEXTAREA element
    • 1 - reference to the second TEXTAREA element
    • 2 - reference to the third TEXTAREA element
    • length - which is 3
    • context - reference to the document object
    • selector - which is 'textarea'
    • plus all those inherited methods...

提交回复
热议问题