什么是 jQuery 对象?
JavaScript 重新定义了数组的含义,因为数组是一个具有 .length
属性和诸如 .slice()
和 .join()
等方法的对象代码>.
jQuery 将 jQuery 对象定义为“类似数组”,因为它具有 length
属性,但它没有诸如 join()
之类的某些数组方法。
如果我将 jQuery 对象定义为一个对象,并且忘记提及与数组有关的任何内容,我将如何定义它?除了长度之外,它还有什么属性呢?
我想所有的方法都是你在文档中看到的,远远超过了数组中方法的数量。
JavaScript kind of redefines what an Array means because an array is an object with a .length
property and methods like .slice()
and .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()
.
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? 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 对象类似于数组,这意味着它包含零个或多个索引(名称是从零开始的正整数的属性)。除了这些索引之外,jQuery 对象还包含以下属性:
length
context
selector
还有大约 140 个继承方法(在
jQuery.prototype
对象 - 您可以执行console.dir(jQuery.prototype)
来获取完整列表...)。请注意,jQuery 对象不包含(或继承)Array 方法(slice、substr...)。如果您想在 jQuery 对象上执行这些方法,请使用
call
/apply
。例如,如果页面上有 3 个 TEXTAREA 元素,并且执行以下操作:
则此
j
jQuery 对象将包含以下属性:0
- 对第一个 TEXTAREA 元素的引用2
- 对第三个 TEXTAREA 元素的引用length
- 即3
context
- 参考document
对象selector
- 即'textarea'
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 doconsole.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:
then this
j
jQuery object will contain these properties:0
- reference to the first TEXTAREA element1
- reference to the second TEXTAREA element2
- reference to the third TEXTAREA elementlength
- which is3
context
- reference to thedocument
objectselector
- which is'textarea'
jQuery 对象是一个具有
长度和数字属性允许对象像这样响应一个数组。您可以在
for
循环中运行它,或者在其上使用map
或each
等函数。我建议使用浏览器的调试器或 Firebug 并检查 jQuery 对象。这将教会您很多关于它的结构的知识。
the jQuery object is an object which has
The length and numeric properties allow the object to respond like an array. You can run it in a
for
loop or use functions likemap
oreach
on it.I would recommend using your browser's debugger or Firebug and inspect a jQuery object. That will teach you a lot about how it is structured.