我是 javascript 的新手,也是 jquery 的新手。
我注意到 jQuery()
选择器返回的 jquery 集与 javascript 数组有很多相似之处,例如jquery集合中的元素可以通过方括号[]
语法访问
所以我想知道,jquery set 和 javascript array 之间有什么根本的区别吗?或者它们可以被认为是表示同一事物的两种方式?
最佳答案
来自他们自己的文档 ( found here ):
The jQuery object itself behaves much like an array; it has a length property and the elements in the object can be accessed by their numeric indices [0] to [length-1]. Note that a jQuery object is not actually a Javascript Array object, so it does not have all the methods of a true Array object such as join().
所以 jquery 对象有点像数组,但它不是数组。基本上,数组将具有 jquery 对象缺少的方法,而 jquery 对象具有数组缺少的方法。我通常会尝试不将 jquery 对象视为数组,而是在尝试使用数组方法时尝试使用 jquery 方法(即,使用 .each
而不是数组方法 。 forEach
).
编辑:
在阅读了更多有关 jquery 对象的信息后,它们确实与数组共享很多方法,例如 .find
。此外,它还提供了一个.toArray
方法,可用于将jquery 对象转换为实际 javascript 数组。
关于javascript - javascript array 和 jquery set 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47522903/