jQuery ID Selector("#id")返回Array [英] jQuery ID Selector ("#id") Returns Array

查看:159
本文介绍了jQuery ID Selector("#id")返回Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在noConflict模式下使用jQuery v1.6.1。

I'm using jQuery v1.6.1 in noConflict mode.

我正在使用id选择器,例如 $ j(#divID ).value 获取存储项的值。

I'm using id selectors such as $j("#divID").value to get values of stored items.

不幸的是, $ j(#inputID)返回一个项目列表,所以我必须使用 $ j(divID)[0] .value 来获取值的宾语。 [0] 似乎是不必要的,因为根据定义,只有一个html元素具有任何给定的id。

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.

这是从IDed对象获取值的适当方法吗?或者有更好的方法吗?

Is this the appropriate way to gets values from an IDed object? Or is there a better way?

谢谢!

推荐答案

$ j(#divID)。val()将正常工作。

根据 jQuery文档 .val()将返回匹配元素集中第一个元素的值。

Per the jQuery documentation, .val() will return the value of the first element in the set of matched elements.

从概念上理解如何理解jQuery的工作原理是为了看看为什么它以这种方式工作。任何选择器查询的结果都是jQuery对象。这是jQuery对象,它包含jQuery提供的无数方法。 .val()是其中一种方法,如 .fadeIn() .hide()等等......这些方法不是DOM对象上的方法,而是jQuery对象的方法。因为jQuery对象是通用的,并且可以在其内部数组中保存0个,1个或多个DOM对象,所以从jQuery选择器调用返回相同的jQuery对象,无论结果中是否包含0个,1个或更多DOM对象。

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.

因此 $ j(#divID)只包含一个对象,返回与<$ c $相同类型的对象c> $ j(。rows),可能包含数百个DOM对象。这极大地简化了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.

当您引用<$ c $时c> $ j(divID)[0] ,您将进入jQuery对象的DOM对象的内部数组(在选择器查询中填充)并获取该数组中的第一个DOM对象。此时,您有一个普通的DOM对象,而不是一个jQuery对象,您可以使用普通的DOM方法或属性。有时这是必需的(获取实际的DOM对象),但通常,只使用jQuery在jQuery对象上提供的方法更容易。使用它们有很多好处,例如你可以将多个请求链接到大多数方法,它会自动迭代它的内部数组中的所有DOM对象。

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(rows-even)。hide()并且该类有20行,那么所有这些行都是由hide()方法操作,没有比这更多的代码。您可以将多个方法链接在一起,如下所示: $ j(rows-even)。slideUp()。slideDown()。在这种情况下,您正在运行动画,jQuery将这两个动画链接在一起,当第一个完成时自动启动第二个动画。它在许多情况下都非常有用,并且可以节省大量代码,而不是通常使用普通JS编写的代码。

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.

这篇关于jQuery ID Selector(&quot;#id&quot;)返回Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆