jQuery : eq() vs get()

前端 未结 8 1181
挽巷
挽巷 2020-11-28 03:50

I\'m new to jQuery, and I\'m wondering what the difference is between jQuery\'s get() and eq() functions. I may misunderstand what the get()

相关标签:
8条回答
  • 2020-11-28 04:38

    Answers above have explained specificly and correctly. I want to add a few points here that might help for the use of get().

    1. If you don't pass an argument to .get(), it will return an Array of the DOM elements.

    2. If you got a DOM object using get(), like var s = $("#id").get(0) you can turn it back into jQuery object simply by using this, $(s)

    3. You can use $obj[i] as an alternative way if you don't want to use $obj.get(i), see below,

      var $obj = $("#ul li");
      var obj1 = $obj.get(1);
      var obj2 = $obj[1];
      
      //true
      return obj2===obj1;
      
    0 讨论(0)
  • 2020-11-28 04:40

    To build on the other answers:

    $('h2').get(0) === $('h2').eq(0)[0]  //true
    $( $('h2').get(0) ) === $('h2').eq(0)  //true
    
    0 讨论(0)
提交回复
热议问题