In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about the difference between eq() and get() in jquery. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
Differences: 1. eq() method returns jquery object, while get() method returns js object;2. eq method can be used directly with other jquery methods, while get() method cannot be used directly. You need to convert the return value to jquery object before you can use jquery method.
This tutorial operates on Windows 7, jquery version 1.10.2, Dell G3 computers.
Difference between eq() and get() in jquery
Before using eq and get methods are arbitrary, but did not delve into the difference:
The eq method returns a jquery object, while the get method returns a js object;jquery objects can use jquery methods, while js objects can only use js native methods, but js objects can be converted to jquery objects and then use jquery methods; for example: access the backcolor of an element:
① Use eq method: $("dv").eq(0).css("backcolor")
② Use the get method: $("dv").get(0).style.backcolor
Let's illustrate with an example:
First introduce the JQuery library file
html
item1 item2 item3
js
$("#b1").on("click",function(){ var $obj = $("#ul li"); $obj.eq(1).css("color","yellow"); $obj.get(2).css("color","red");})
At this time, click the second li of the button, that is, the font of item2 turns yellow, but item3 does not turn red, and the following error is reported:
The error means that $obj doesn't have a get() method because it's an array of DOM objects, and it doesn't have a get() method, so how do we turn it into a JQuery object?
Just change $obj.get(2) to $($obj.get(2)),
$("#b1").on("click",function(){ var $obj = $("#ul li"); $obj.eq(1).css("color","yellow"); $($obj.get(2)).css("color","red");})
When you click the button again, the following screen will appear:
After verification, after this example, I believe that everyone will understand the eq() and get() methods.
Finally, let's extend it, or based on html above
$("#b1").on("click",function(){ var $obj = $("#ul li"); var obj1 = $obj.get(1); var obj2 = $obj[1]; if(obj2===obj1){ alert(111); }else{ alert(222); }})
Can you guess what popped up?
After verifying that 111 pops up, we can conclude that $obj.get(1) and $obj[1] can be used interchangeably here.
jquery object and Dom object conversion method:
1. jquery object-->Dom object:
There are two ways:
jquery object.get()
For example:
var v = $("#id").get(0);//Dom object
[0][0]
For example:
var v = $("#id")[0];//Dom object
2. Dom object-->jquery object:
$(Dom object), for example:
var $v =$(document.getelementbyid("id"));//jquery object Thank you for reading! About "what is the difference between eq() and get() in jquery" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.