The corresponding DOM element is obtained by retrieving the matching jquery object.
. get ([index])
- index Type: integer is counted starting at 0 to determine which element to get.
.get()
method allows us to directly access the relevant DOM nodes in the jquery object. If index
the value is out of range-less than the number of elements in the negative or equal to or greater than the number of elements-then it returns undefined
. Let's say we have a simple unordered list on our page:
< ul > < ID= "foo">foo</li> < id = "Bar" > Bar </li > </ ul >
If the index parameter is specified,. Get () gets a single element:
Console.log ($ ("Li"). Get (0));
The above code returns the first list item because the index is counted starting at 0:
<li id= "foo" >
Each JQuery object is also treated as an array, so we can also get the list item directly with the array's value operator:
Console.log ($ ("li") [0]);
However, this syntax lacks the additional functionality of some. get (), such as the ability to specify a negative index value:
Console.log ($ ("Li"). Get (-1));
A negative index value indicates the countdown from the end of the matching collection, so the above example will return the last item in the list:
<li id= "Bar" >
Example:gives the label name of the element in the point.
<!DOCTYPE HTML><HTML><Head> <style>span{Color:Red; }Div{background:Yellow; } </style> <Scriptsrc= "Http://code.jquery.com/jquery-latest.js"></Script></Head><Body> <span> </span> <P>In this paragraph are an<span>Important</span>Section</P> <Div><inputtype= "text" /></Div><Script>$("*", Document.body). Click (function(e) {e.stoppropagation (); varDomel= $( This). Get (0); $("Span:first"). Text ("Clicked on-" +domel.tagname);});</Script> </Body></HTML>
Description: gets the corresponding DOM element by retrieving the matching jquery object.
- Version Added:1.0.get ()
- This method does not receive any parameters
Let's say we have a simple unordered list on our page:
< ul > < ID= "foo">foo</li> < id = "Bar" > Bar </li > </ul>
If the parameter is not taken, .get()
all elements are returned:
Console.log ($ ("Li"). get ());
Calling this method will return all matching DOM nodes, which are contained in a standard array:
[<li id= "foo", <li id= "bar";]
Example:selects all the div in the document and returns as an array. Then use the native reverse method of the browser to flip the array.
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "Utf-8"> <title>Get Demo</title> <style>span{Color:Red; } </style> <Scriptsrc= "Http://code.jquery.com/jquery-1.9.1.js"></Script></Head><Body>Reversed-<span></span> <Div>One</Div> <Div>Both</Div> <Div>Three</Div><Script> functiondisp (divs) {vara= []; for ( varI= 0; I<divs.length; I++) {A.push (divs[i].innerhtml); } $( "span"). Text (A.join (" ") ); } disp ($ ("Div"). Get (). reverse ()); </Script> </Body></HTML>
The Get () method of jquery