I have never understood eq in jquery, but I found that eq is used in many places. I will share with you several examples below.
Definition and usage
: The eq () selector selects an element with the specified index value.
The index value starts from 0, and the index value of all the first element is 0 (not 1 ).
It is often used with other elements/selectors to select elements with specific sequence numbers in the specified group (in the preceding example ).
The Code is as follows: |
Copy code |
<P> <Label for = ""> Gender: </label> <Span> <Em> male </em> <Input type = "radio" name = "sex" value = "male"/> </Span> <Span> <Em> female </em> <Input type = "radio" name = "sex" value = "female"/> </Span> </P> |
When jQuery sets the radio value, we can see that someone uses the nth method.
? Use nth
The Code is as follows: |
Copy code |
$ ('Input: radio [name = sex]: nth (0) '). attr ('checked', true ); |
In fact, nth and eq are the same and can be replaced with eq.
The Code is as follows: |
Copy code |
Var sex = 'female '; If (sex = 'male '){ $ ("Input: radio [name = 'sex ']: eq (0)"). attr ('checked', true ); } Else { $ ("Input: radio [name = 'sex ']: eq (1)"). attr ('checked', true ); } |
In jQuery source code, we can see that nth is not recommended for use. If used, it will be converted to eq.
The Code is as follows: |
Copy code |
// Deprecated Expr. pseudo dos ["nth"] = Expr. pseudo dos ["eq"]; |