For example:
<P style = "color: yellow"> Yu </p> uses eq to obtain the color value of the first p Tag:
$ ("P" ).eq(0).css ("color") // because eq (num) returns a jq object, therefore, you can use the jq method css to get the color value of the first p tag using get:
$ ("P "). get (0 ). style. color // because get (num) returns an html object, it is useless to use the traditional HTML object method. Of course, you can also get (num) and convert the object into the jq object before performing the operation:
$ ("P" ).get(0)).css ("color ")--------------------------------------------------------------------------
More eq
See:
Http://api.jquery.com/eq/
--------------------------------------------------------------------------
More get:
See:
Http://api.jquery.com/get/
Eq: The returned jquery object is used to reduce the Matching Element Set to an element. The position of this element in the Matching Element Set is changed to 0, and the set length is changed to 1.
Get: an html object array is used to obtain a matching element. Num indicates the number of matched elements.
For example, html code
Copy codeThe Code is as follows:
<Ul>
<Li> li-1 </li>
<Li> li-2 </li>
</Ul>
For example, if we use the jquery selector $ ("li"), then we will have two li elements. How can I select only one of them?
$ ("Li: eq (0)" ).html () or $ ("li" ).eq(0).html () is the first li here we will get the li-1
$ ("Li: eq (1)" ).html () or $ ("li" ).eq(1).html () is the second li here we will get li-2
Next let's take a look at get. Because get returns html objects, we are here
$ ("Li"). get (0). style. color = 'red'
You can only use this method or convert the get returned object to a jquery object in the operation.
$ ("Li" ).get(0)).css ("color", 'red ')
Complete code
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script language = "JavaScript" src = "js/jquery. js"> </script>
<Script language = "JavaScript">
<! --
$ ("Document"). ready (function (){
Alert ($ ("li: eq (1)" ).html (); // display li-2 alert ($ ("li: eq (0)" ).html ())
$ ("Li"). get (0). style. color = 'red ';
$ ("Li" ).get(1)).css ("color", 'red ')
})
// -->
</SCRIPT>
</HEAD>
<BODY>
<Ul>
<Li> li-1 </li>
<Li> li-2 </li>
</Ul>
</BODY>
</HTML>