Home >Web Front-end >Front-end Q&A >How to select the first few elements in jquery
In jquery, you can use the ":lt()" selector to select the first few elements. This selector is used to select elements whose index value is less than the specified number. This selector is often used together with other selectors. You can select the elements before a specific sequence number in the specified combination, and the syntax is "$(":lt(index)")".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
:lt() selector selects elements whose index value is less than the specified number.
index values start from 0.
The most common usage: used with other selectors to select elements before a specific sequence number in the specified combination (such as the example above).
On the contrary, you can use :gt selector to select elements whose index value is greater than the specified number.
Syntax
$(":lt(index)")
index Required. Specifies the elements to be selected. Elements whose index value is less than the specified number will be selected.
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("tr:lt(4)").css("background-color", "yellow"); }); </script> </head> <body> <h1>欢迎来到我的主页</h1> <table border="1"> <tr> <th>序号</th> <th>站点名</th> <th>网址</th> </tr> <tr> <td>1</td> <td>Google</td> <td>google.com</td> </tr> <tr> <td>2</td> <td>PHP</td> <td>php.cn</td> </tr> <tr> <td>3</td> <td>Taobao</td> <td>taobao.com</td> </tr> <tr> <td>4</td> <td>Baidu</td> <td>baidu.com</td> </tr> <tr> <td>5</td> <td>Sina</td> <td>sina.com.cn</td> </tr> <tr> <td>6</td> <td>Facebook</td> <td>facebook.com</td> </tr> <tr> <td>7</td> <td>twitter</td> <td>twitter.com</td> </tr> <tr> <td>8</td> <td>youtube</td> <td>youtube.com</td> </tr> </table> </body> </html>
Output result:
Related video tutorial recommendation: jQuery video tutorial
The above is the detailed content of How to select the first few elements in jquery. For more information, please follow other related articles on the PHP Chinese website!