简体   繁体   English

什么是jQuery对象?

[英]What is a jQuery Object?

JavaScript kind of redefines what an Array means because an array is an object with a .length property and methods like .slice() and .join() . JavaScript类重新定义了Array的含义,因为数组是具有.length属性的对象,以及.slice().join()

jQuery defines a jQuery object as "Array like", because it has a length property but it doesn't have certain array methods like join() . jQuery将jQuery对象定义为“Array like”,因为它具有length属性,但它没有某些数组方法,如join()

If I were to define the jQuery object as an object, and forget about mentioning anything to do with an array, how would I define it? 如果我将jQuery对象定义为对象,并且忘记提及与数组有关的任何内容,我将如何定义它? What properties does it have besides length? 除长度外还有哪些属性?

I guess all the methods are what you see in the documentation, far exceeding the number of methods that are in an array. 我猜所有的方法都是你在文档中看到的,远远超过了数组中的方法数量。

A jQuery object is array-like which means that it contains zero or more indexes (properties which names are positive integers starting with zero). jQuery对象是类似数组的,这意味着它包含零个或多个索引(名称是从零开始的正整数的属性)。 Besides those indexes, a jQuery object contains these properties: 除了这些索引之外,jQuery对象还包含以下属性:

  • 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... ). 还有大约140个继承的方法(在jQuery.prototype对象上定义 - 你可以做console.dir(jQuery.prototype)来获得一个完整的列表......)。

Note that jQuery objects do not contain (or inherit) the Array methods (slice, substr, ...). 请注意,jQuery对象包含(或继承)Array方法(slice,substr,...)。 If you want to execute those methods on your jQuery object, use call / apply . 如果要在jQuery对象上执行这些方法,请使用call / apply


For example, if you have 3 TEXTAREA elements on the page and you do this: 例如,如果页面上有3个TEXTAREA元素,则执行此操作:

var j = $('textarea');

then this j jQuery object will contain these properties: 那么这个j jQuery对象将包含以下属性:

  • 0 - reference to the first TEXTAREA element 0 - 对第一个TEXTAREA元素的引用
  • 1 - reference to the second TEXTAREA element 1 - 引用第二个TEXTAREA元素
  • 2 - reference to the third TEXTAREA element 2 - 对第三个TEXTAREA元素的引用
  • length - which is 3 length - 这是3
  • context - reference to the document object context - 对document对象的引用
  • selector - which is 'textarea' selector - 这是'textarea'
  • plus all those inherited methods... 加上所有那些继承的方法......

the jQuery object is an object which has jQuery对象是一个具有的对象

  • a length property 一个长度的财产
  • numeric properties which reference the items from the select (0,1,2,3...) 引用select(0,1,2,3 ...)项的数字属性
  • bindings to jQuery functions 绑定到jQuery函数
  • additional jQuery properties 额外的jQuery属性

The length and numeric properties allow the object to respond like an array. length和numeric属性允许对象像数组一样响应。 You can run it in a for loop or use functions like map or each on it. 您可以在for循环中运行它,也可以使用map或其中的each函数。

I would recommend using your browser's debugger or Firebug and inspect a jQuery object. 我建议使用浏览器的调试器或Firebug并检查jQuery对象。 That will teach you a lot about how it is structured. 这将教会你很多关于它的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM