jquery $ 实际返回什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1302428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does jquery $ actually return?
提问by harpo
I have read the JQuery documentation, and while much attention is devoted to what you should passthe function, I don't see any information on what it actually returns.
我已经阅读了 JQuery 文档,虽然很多注意力都集中在应该传递函数的内容上,但我没有看到有关它实际返回内容的任何信息。
In particular, does it always return an array, even if only one element is found? Does it return null when nothing is found? Where is this documented?
特别是,它是否总是返回一个数组,即使只找到一个元素?当什么都没有找到时它是否返回null?这是在哪里记录的?
I understand that jquery methods can be applied to the return value, but what if I want to just use the return value directly?
我知道jquery方法可以应用于返回值,但是如果我只想直接使用返回值怎么办?
采纳答案by Ian Robinson
From Rick Strahl's description:
The jQuery Object: The Wrapped Set: Selectors return a jQuery object known as the "wrapped set," which is an array-like structure that contains all the selected DOM elements. You can iterate over the wrapped set like an array or access individual elements via the indexer ($(sel)[0] for example). More importantly, you can also apply jQuery functions against all the selected elements.
jQuery 对象:包装集:选择器返回一个称为“包装集”的 jQuery 对象,它是一个类似数组的结构,包含所有选定的 DOM 元素。您可以像数组一样遍历包装集或通过索引器访问单个元素(例如 $(sel)[0])。更重要的是,您还可以对所有选定元素应用 jQuery 函数。
About returning nothing:
关于什么都不返回:
Does it always return an array? Does it return null?
它总是返回一个数组吗?它返回空值吗?
You always get the same thing back, whether or not it has any contents is the question. Typically you can check this by using .val() (e.g. $('.myElem').val())
你总是得到同样的东西,它是否有任何内容是个问题。通常,您可以使用 .val() 来检查这一点(例如 $('.myElem').val())
回答by John Millikin
It doesn't return an array, it returns a jQuery object. The jQuery object is what contains all the special jQuery methods.
它不返回一个数组,它返回一个 jQuery 对象。jQuery 对象包含所有特殊的 jQuery 方法。
It never returns null, or another type. If one element is found, the jQuery object will have only one child. If no elements are found, the jQuery object will be empty.
它从不返回 null 或其他类型。如果找到一个元素,jQuery 对象将只有一个子元素。如果没有找到元素,jQuery 对象将为空。
回答by Andrew Hare
The jQuery function (i.e. "$
") always returns a jQuery object in every instance.
jQuery 函数(即“ $
”)总是在每个实例中返回一个 jQuery 对象。
回答by TM.
As another answerer mentioned, it alwaysreturns the jQuery object.
正如另一个回答者提到的,它总是返回 jQuery 对象。
This object always contains an array of elements (even if it is an empty array, or an array with just one object).
这个对象总是包含一个元素数组(即使它是一个空数组,或者一个只有一个对象的数组)。
If you'd like to use the returned object "directly", as in, as a plain element, you can do one of the following:
如果您想“直接”使用返回的对象,例如作为普通元素,您可以执行以下操作之一:
$('selector')[0] // element
$('selector').get(0) // element
$('selector').length // number of elements in the array
回答by d512
From the jQuery documentation:
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 对象本身的行为很像一个数组;它有一个长度属性,对象中的元素可以通过它们的数字索引 [0] 到 [length-1] 来访问。请注意,jQuery 对象实际上不是 Javascript Array 对象,因此它没有真正的 Array 对象的所有方法,例如 join()。
回答by Stefan Kendall
The fact that $() always returns the jQuery function lets you chain jQuery function calls judiciously.
$() 始终返回 jQuery 函数这一事实让您可以明智地链接 jQuery 函数调用。
回答by Eric Wang
Jquery selector mechnism
Jquery选择器机制
$("..")
, the jquery selector, is used to select matched elements.
$("..")
,jquery 选择器,用于选择匹配的元素。
Return value
返回值
It always return an array-like jquery object, which has a length
property,
它总是返回一个类似数组的 jquery 对象,它有一个length
属性,
Call method on returned jquery object
在返回的 jquery 对象上调用方法
Methods of jquery could be called on the object, and apply to those selected elements,
可以在对象上调用 jquery 的方法,并应用于那些选定的元素,
Access original element by index
按索引访问原始元素
The selected elements, are store as property of the object, their property name are index numbers start from 0,
thus could be accessed by index, start from 0,
after get the original element, you can treat it as if get by document.getElementXxx()
.
被选中的元素,作为对象的属性存储,它们的属性名是从0开始的索引号,
因此可以通过索引访问,从0开始,
在获取原始元素后,您可以将其视为get by document.getElementXxx()
。
Wrap an original element to a jquery object
将原始元素包装到 jquery 对象
After get the original element, you can wrap it to be a jquery object, by calling $(originalEle)
,
then you can call jquery methods on the wrapped object.
获取原始元素后,您可以将其包装为jquery对象,通过调用$(originalEle)
,
然后您可以在包装的对象上调用jquery方法。
回答by jrsconfitto
Their documentationlists a few of the core calls you can use with "$" and what they return
他们的文档列出了一些可以与“$”一起使用的核心调用以及它们返回的内容
回答by eKek0
According to firebug, it returns an array of objects that match to your selector. But this array is a jQuery object, that more methods than a simple Array.
根据萤火虫,它返回与您的选择器匹配的对象数组。但是这个数组是一个 jQuery 对象,比简单的 Array 有更多的方法。