Javascript jQuery ID 选择器 ("#id") 返回数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7183704/
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
jQuery ID Selector ("#id") Returns Array
提问by Richard
I'm using jQuery v1.6.1 in noConflict mode.
我在 noConflict 模式下使用 jQuery v1.6.1。
I'm using id selectors such as $j("#divID").value
to get values of stored items.
我正在使用 id 选择器$j("#divID").value
来获取存储项目的值。
Unfortunately, $j("#inputID")
is returning a list of items, so I have to use the $j("divID")[0].value
to get the value of the object. The [0]
seems unnecessary, since there is, by definition, only one html element with any given id.
不幸的是,$j("#inputID")
正在返回一个项目列表,所以我必须使用$j("divID")[0].value
来获取对象的值。这[0]
似乎是不必要的,因为根据定义,只有一个具有任何给定 id 的 html 元素。
Is this the appropriate way to gets values from an IDed object? Or is there a better way?
这是从 IDed 对象获取值的合适方法吗?或者,还有更好的方法?
Thanks!
谢谢!
回答by jfriend00
$j("#divID").val()
will work just fine.
$j("#divID").val()
会工作得很好。
Per the jQuery documentation, .val()
will return the value of the first element in the set of matched elements.
根据jQuery 文档,.val()
将返回匹配元素集中第一个元素的值。
It's worthwhile understanding conceptually how jQuery works in order to see why it works this way. The result of any selector query is a jQuery object. It's that jQuery object that contains the myriad of methods that jQuery offers. .val()
is one of those methods as are things like .fadeIn()
, .hide()
, etc... Those methods are not methods on a DOM object, but methods of a jQuery object. Because jQuery objects are general purpose and can hold 0, 1 or more DOM objects in their internal array, you get the same jQuery object back from a jQuery selector call whether the results have 0, 1 or more DOM objects in it.
有必要从概念上理解 jQuery 的工作原理,以便了解它为何以这种方式工作。任何选择器查询的结果都是一个 jQuery 对象。正是这个 jQuery 对象包含了 jQuery 提供的无数方法。 .val()
是其中一种方法,如.fadeIn()
、.hide()
等...这些方法不是 DOM 对象上的方法,而是 jQuery 对象的方法。因为 jQuery 对象是通用的并且可以在其内部数组中保存 0、1 或更多 DOM 对象,所以无论结果中是否包含 0、1 或更多 DOM 对象,您都可以从 jQuery 选择器调用中获得相同的 jQuery 对象。
Thus $j("#divID")
that contains only one object returns the same type of object as $j(".rows")
which might contain hundreds of DOM objects. This vastly simplifies jQuery programming because you don't have to do things differently depending upon how many objects come back from the selector query.
因此$j("#divID")
,只包含一个对象的对象返回的对象类型与$j(".rows")
可能包含数百个 DOM 对象的对象类型相同。这极大地简化了 jQuery 编程,因为您不必根据从选择器查询返回的对象数量来做不同的事情。
When you refer to $j("divID")[0]
, you are reaching into the jQuery object's internal array of DOM objects (that was populated on the selector query) and fetching the first DOM object in that array. At that point, you have a normal DOM object, not a jQuery object and you can use normal DOM methods or attributes on it. Occasionally this is required (to fetch the actual DOM object), but usually, it's easier to just use the methods that jQuery provides on the jQuery object. There are lots of advantages to using them such as you can chain multiple requests to most methods and it will iterate over all the DOM objects in it's internal array for you automatically.
当您引用 时$j("divID")[0]
,您将进入 jQuery 对象的内部 DOM 对象数组(在选择器查询中填充)并获取该数组中的第一个 DOM 对象。那时,您有一个普通的 DOM 对象,而不是一个 jQuery 对象,您可以在其上使用普通的 DOM 方法或属性。有时这是必需的(以获取实际的 DOM 对象),但通常,仅使用 jQuery 在 jQuery 对象上提供的方法更容易。使用它们有很多优点,例如您可以将多个请求链接到大多数方法,并且它会自动为您迭代其内部数组中的所有 DOM 对象。
For example, you you called this: $j("rows-even").hide()
and there were 20 rows with that class, then all of them would each be operated on by the hide() method with no more code than this. Of you could chain multiple methods together like this: $j("rows-even").slideUp().slideDown()
. In this case, you're running an animation and jQuery will chain these two animations together, automatically starting the second one when the first one finishes. It's all pretty useful in many circumstances and can save a ton of code over what would normally have to be written using plain JS.
例如,您调用了这个:$j("rows-even").hide()
并且有 20 行具有该类,那么所有这些行都将通过 hide() 方法进行操作,代码仅此而已。你的可能连锁多种方法一起这样的: $j("rows-even").slideUp().slideDown()
。在本例中,您正在运行一个动画,jQuery 会将这两个动画链接在一起,并在第一个动画完成时自动启动第二个动画。它在许多情况下都非常有用,并且可以比通常必须使用普通 JS 编写的代码节省大量代码。
回答by ShankarSangoli
$j("#divID")
returns a jQuery
object. In order to get the value of the selected element you have to call its val
method to get the value.
$j("#divID")
返回一个jQuery
对象。为了获取所选元素的值,您必须调用其val
方法来获取值。
Use $j("#divID").val();
用 $j("#divID").val();