How does jquery choose the third p element
This article mainly introduces "how jquery chooses the third p element". In daily operation, I believe many people have doubts about how to choose the third p element in jquery. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how jquery chooses the third p element". Next, please follow the editor to study!
The method of selecting the third p element: 1, use eq () to select the p element with index number 2, syntax "$(" p "). Eq (2)"; 2, use ": eq ()" to select the p element with index number 2, syntax "$(" p:eq (2) ")". The element index number is counted from 0, so the index number of the third element is 2.
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
Jquery selects the third p element
1. Using eq () method
The eq () method returns the element with the specified index number of the selected element. The index number starts with 0, so the index number of the first element is 0 (not 1).
So to choose the third p element is to select the p element with index number 2.
$(document) .ready (function () {$("button") .click (function () {$("p") .eq (2) .css ({"color": "red") "border": "2px solid red"}) }); title
The first p element
The second p element
Span element
The third p element
The fourth p element
The fifth p element
Select the third p element
2. Using the ": eq ()" selector
The eq () selector is similar to the eq () method in that it selects elements with a specified index. The index also starts at 0, so the index number of the first element is 0 (not 1).
Similarly, use: eq () to select the p element with index number 2.
$(document) .ready (function () {$("button") .click (function () {$("p:eq (2)") .css ({"color": "red") "border": "2px solid pink"}) }); title
The first p element
The second p element
Span element
The third p element
The fourth p element
The fifth p element
Select the third p element
At this point, the study on "how to choose the third p element of jquery" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!