javascript - Is jQuery's $(selector).eq(index) and $(selector)[index] the same? - Stack Overflow

What is the difference between all these ways?1$('div').eq(index) 2$('div')[i

What is the difference between all these ways?

//1
$('div').eq(index) 

//2
$('div')[index] 

//3
$($('div')[index] )

//4
$('div').get(1)

Are they same?

What is the difference between all these ways?

//1
$('div').eq(index) 

//2
$('div')[index] 

//3
$($('div')[index] )

//4
$('div').get(1)

Are they same?

Share Improve this question edited Dec 7, 2012 at 14:36 VisioN 145k34 gold badges287 silver badges289 bronze badges asked Dec 7, 2012 at 11:45 Akhil SekharanAkhil Sekharan 12.7k8 gold badges42 silver badges58 bronze badges 3
  • Of course. I got the same output in chrome developer console – Akhil Sekharan Commented Dec 7, 2012 at 11:48
  • Then something is seriously wrong with your browser, call Google and plain at once! – adeneo Commented Dec 7, 2012 at 11:52
  • @adeneo Ah, you angry ;) – VisioN Commented Dec 7, 2012 at 14:35
Add a ment  | 

4 Answers 4

Reset to default 5

No.

The first and the third return jQuery object, while the second and the forth return DOM element:

$("div").eq(index) === $($("div")[index]);  // --> jQuery object

$("div")[index] === $("div").get(index);    // --> DOM element

You can read about the last case here: http://api.jquery./get/.

If you need to see the difference between types you may try to run the following in the console:

Object.prototype.toString.call($("div").eq(index)); // "[object Object]"
Object.prototype.toString.call($("div")[index]);    // "[object HTMLDivElement]"

First and third will get you jQuery object and second one will give you DOM object.

  1. $('div').eq(index) // will return jquery object

  2. $('div')[index] // will give you javascript DOM object

  3. $($('div')[index]) //will give you jQuery object by converting DOM object

You require different syntax to get the properties for instance for id of object.

idofobject = $('div').eq(index).attr('id') //jQuery

idofobject = $('div')[index].id            //javascript

$('div').eq(index) This returns a JQuery object

$('div')[index] this will give you javascript object

  • v1: $('div').eq(index) :: returns a jQuery-wrapped collection prising one div.

  • v2: $('div')[index] :: returns a reference to a DOM element (not jQuery-wrapped)

  • v3: $($('div')[index]) :: returns a jQuery-wrapped collection prising one div. This is a verbose and inefficient version of the v1.

You didn't ask about $('div').get(index), which is another way of achieving v2. AFAIK, it is not substantially less efficient.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742359923a4429186.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信