jQuery:数组零与函数得到零:[0]与get(0) [英] jQuery: array zero vs function get zero: [0] vs get(0)

查看:229
本文介绍了jQuery:数组零与函数得到零:[0]与get(0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我可以只使用$('#x>div')[1]时,有什么理由应该使用$('#x>div').get(1)吗?有区别吗?

Is there any reason I should use $('#x>div').get(1) when I could instead just use $('#x>div')[1]? Is there a difference?

推荐答案

不,没有区别. jQuery将所有DOM节点保存在一个数组中.

Nope, no difference. jQuery holds all DOM nodes in an Array.

$().get(1) === $()[1]

-jQuery源代码片段-

--jQuery source snippet--

get: function( num ) {
    return num == null ?
        // Return a 'clean' array
        this.toArray() :

        // Return just the object
        ( num < 0 ? this[ this.length + num ] : this[ num ] );
},

如您所见,不带任何参数的.get()会将所有节点返回为Array.这不能用括号来完成.

As you can see, .get() with no arguments will return all nodes as Array. This cannot be accomplished with brackets.

这篇关于jQuery:数组零与函数得到零:[0]与get(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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