Is there any reason I should use $('#x>div').get(1)
when I could instead just use $('#x>div')[1]
? Is there a d开发者_如何转开发ifference?
Nope, no difference. jQuery holds all DOM nodes in an Array.
$().get(1)
=== $()[1]
--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 ] );
},
As you can see, .get()
with no arguments will return all nodes as Array. This cannot be accomplished with brackets.
No, and performance is about the same because the creation of a jQuery object dominates array/function access time:
Browser get Ops/sec array Ops/sec #tests
Chrome 9 20,555 22,671 2
精彩评论