jQuery:数组零vs函数得零: vs get(0)
有什么理由我应该使用$('#x>div').get(1)
当我可以改为只使用$('#x>div')[1]
? 有区别吗?
不,没有区别。 jQuery将所有DOM节点保存在一个Array中。
$().get(1)
=== $()[1]
–jQuery源代码段 –
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。 这不能用括号来完成。
不,并且性能大致相同,因为创建jQuery对象支配数组/函数访问时间:
Browser get Ops/sec array Ops/sec #tests Chrome 9 20,555 22,671 2