如何获得第一个元素而不是在jQuery中使用[0]? [英] How do I get first element rather than using [0] in jQuery?

查看:105
本文介绍了如何获得第一个元素而不是在jQuery中使用[0]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手,如果这是一个愚蠢的问题,请道歉。



当我使用它时使用id找到一个元素,我知道总是一个匹配并且为了访问它,我将使用索引[0]。有没有更好的方法呢?
例如。

  var gridHeader = $(#grid_GridHeader)[0]; 


解决方案

你可以使用 .get(0) 但是......你不应该使用ID找到的元素执行此操作,始终应该是唯一的。我希望这只是示例中的一个疏忽...如果您的实际页面上出现这种情况,您需要修复它,以便您的ID是唯一的,并使用类(或其他属性)代替。 / p>

.get() (如 [0] )获取DOM元素,如果你想使用jQuery对象 .eq(0) .first() 代替:)


I'm new to jQuery, apologies if this is a silly question.

When I use it find an element using the id, I know theres always one match and in order to access it I would use the index [0]. Is there a better way of doing this? For e.g.

var gridHeader = $("#grid_GridHeader")[0];

解决方案

You can use .get(0) as well but...you shouldn't need to do that with an element found by ID, that should always be unique. I'm hoping this is just an oversight in the example...if this is the case on your actual page, you'll need to fix it so your IDs are unique, and use a class (or another attribute) instead.

.get() (like [0]) gets the DOM element, if you want a jQuery object use .eq(0) or .first() instead :)

这篇关于如何获得第一个元素而不是在jQuery中使用[0]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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