Blogger Information
Blog 25
fans 0
comment 0
visits 29644
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jquery选择器
宿州市筋斗云信息科技-Vip
Original
985 people have browsed it

jquery中的选择器!


:first选择器 (选择多个相同元素中的第一个元素!)

:first伪类选择器相当于:eq(0)。它也可以写为:lt(1)。虽然:first只匹配一个单独的元素,但是:first-child选择器可以匹配多个:即为每个父级元素匹配第一个子元素。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>学习笔记-Jquery</title>
  6. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  7. <style type="text/css">
  8. *{
  9. padding: 0;
  10. margin: 0;
  11. }
  12. div {
  13. width: 500px;
  14. height: 60px;
  15. border: 1px solid;
  16. border-color: #000000;
  17. margin: 15px auto 0 auto;
  18. text-align: center;
  19. }
  20. .addCss {
  21. border-color: orangered;
  22. background: red;
  23. color: #FFF;
  24. font-size: 30px;
  25. }
  26. p {
  27. width: 500px;
  28. height: 50px;
  29. text-align: center;
  30. line-height: 50px;
  31. margin: 30px auto 0 auto;
  32. border: 1px solid #eee;
  33. background: #0356fa;
  34. cursor: pointer;
  35. color: #FFF;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div>1</div>
  41. <div>2</div>
  42. <div>3</div>
  43. <div>4</div>
  44. <div>5</div>
  45. <div>6</div>
  46. <p type="button" onclick="clicks()">:first (点我选择第一个DIV)</p>
  47. </body>
  48. <script type="text/javascript">
  49. function clicks(){
  50. $('div:first').addClass('addCss');
  51. }
  52. </script>
  53. </html>

:last选择器 (选择多个相同元素中的最后一个元素!)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>学习笔记-Jquery</title>
  6. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  7. <style type="text/css">
  8. *{
  9. padding: 0;
  10. margin: 0;
  11. }
  12. div {
  13. width: 500px;
  14. height: 60px;
  15. border: 1px solid;
  16. border-color: #000000;
  17. margin: 15px auto 0 auto;
  18. text-align: center;
  19. }
  20. .addCss {
  21. border-color: orangered;
  22. background: red;
  23. color: #FFF;
  24. font-size: 30px;
  25. line-height: 60px;
  26. }
  27. p {
  28. width: 500px;
  29. height: 50px;
  30. text-align: center;
  31. line-height: 50px;
  32. margin: 30px auto 0 auto;
  33. border: 1px solid #eee;
  34. background: #0356fa;
  35. cursor: pointer;
  36. color: #FFF;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div>1</div>
  42. <div>2</div>
  43. <div>3</div>
  44. <div>4</div>
  45. <div>5</div>
  46. <div>6</div>
  47. <p type="button" onclick="clicks()">$('div:last')(点我选择最后一个DIV)</p>
  48. </body>
  49. <script type="text/javascript">
  50. function clicks(){
  51. $('div:last').addClass('addCss');
  52. }
  53. </script>
  54. </html>

:eq()选择器 (选择多个相同元素中的指定的一个元素!从0开始)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>学习笔记-Jquery</title>
  6. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  7. <style type="text/css">
  8. *{
  9. padding: 0;
  10. margin: 0;
  11. }
  12. div {
  13. width: 500px;
  14. height: 60px;
  15. border: 1px solid;
  16. border-color: #000000;
  17. margin: 15px auto 0 auto;
  18. text-align: center;
  19. }
  20. .addCss {
  21. border-color: orangered;
  22. background: red;
  23. color: #FFF;
  24. font-size: 30px;
  25. line-height: 60px;
  26. }
  27. p {
  28. width: 500px;
  29. height: 50px;
  30. text-align: center;
  31. line-height: 50px;
  32. margin: 30px auto 0 auto;
  33. border: 1px solid #eee;
  34. background: #0356fa;
  35. cursor: pointer;
  36. color: #FFF;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div>1</div>
  42. <div>2</div>
  43. <div>3</div>
  44. <div>4</div>
  45. <div>5</div>
  46. <div>6</div>
  47. <p type="button" onclick="clicks()"> $('div:eq(2)')(点我选择) </p>
  48. </body>
  49. <script type="text/javascript">
  50. function clicks(){
  51. $('div:eq(2)').addClass('addCss');
  52. }
  53. </script>
  54. </html>

:lt(index)选择器 (选择匹配集合中所有索引值小于给定index参数的元素。)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>学习笔记-Jquery</title>
  6. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  7. <style type="text/css">
  8. *{
  9. padding: 0;
  10. margin: 0;
  11. }
  12. div {
  13. width: 500px;
  14. height: 60px;
  15. border: 1px solid;
  16. border-color: #000000;
  17. margin: 15px auto 0 auto;
  18. text-align: center;
  19. }
  20. .addCss {
  21. border-color: orangered;
  22. background: red;
  23. color: #FFF;
  24. font-size: 30px;
  25. line-height: 60px;
  26. }
  27. p {
  28. width: 500px;
  29. height: 50px;
  30. text-align: center;
  31. line-height: 50px;
  32. margin: 30px auto 0 auto;
  33. border: 1px solid #eee;
  34. background: #0356fa;
  35. cursor: pointer;
  36. color: #FFF;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div>0</div>
  42. <div>1</div>
  43. <div>2</div>
  44. <div>3</div>
  45. <div>4</div>
  46. <div>5</div>
  47. <p type="button" onclick="clicks()"> $('div:lt(2)')(点我选择) </p>
  48. </body>
  49. <script type="text/javascript">
  50. function clicks(){
  51. $('div:lt(2)').addClass('addCss');
  52. }
  53. </script>
  54. </html>

:gt(index)选择器 (选择匹配集合中所有索引值大于给定index参数的元素。)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>学习笔记-Jquery</title>
  6. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  7. <style type="text/css">
  8. *{
  9. padding: 0;
  10. margin: 0;
  11. }
  12. div {
  13. width: 500px;
  14. height: 60px;
  15. border: 1px solid;
  16. border-color: #000000;
  17. margin: 15px auto 0 auto;
  18. text-align: center;
  19. }
  20. .addCss {
  21. border-color: orangered;
  22. background: red;
  23. color: #FFF;
  24. font-size: 30px;
  25. line-height: 60px;
  26. }
  27. p {
  28. width: 500px;
  29. height: 50px;
  30. text-align: center;
  31. line-height: 50px;
  32. margin: 30px auto 0 auto;
  33. border: 1px solid #eee;
  34. background: #0356fa;
  35. cursor: pointer;
  36. color: #FFF;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div>0</div>
  42. <div>1</div>
  43. <div>2</div>
  44. <div>3</div>
  45. <div>4</div>
  46. <div>5</div>
  47. <p type="button" onclick="clicks()"> $('div:gt(0)')(点我选择) </p>
  48. </body>
  49. <script type="text/javascript">
  50. function clicks(){
  51. $('div:gt(0)').addClass('addCss');
  52. }
  53. </script>
  54. </html>

:DIV[ID]选择所有具有指定属性的元素,该属性可以是任何值。
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>学习笔记-Jquery</title>
  6. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  7. <style type="text/css">
  8. *{
  9. padding: 0;
  10. margin: 0;
  11. }
  12. div {
  13. width: 500px;
  14. height: 60px;
  15. border: 1px solid;
  16. border-color: #000000;
  17. margin: 15px auto 0 auto;
  18. text-align: center;
  19. }
  20. .addCss {
  21. border-color: orangered;
  22. background: red;
  23. color: #FFF;
  24. font-size: 30px;
  25. line-height: 60px;
  26. }
  27. p {
  28. width: 500px;
  29. height: 50px;
  30. text-align: center;
  31. line-height: 50px;
  32. margin: 30px auto 0 auto;
  33. border: 1px solid #eee;
  34. background: #0356fa;
  35. cursor: pointer;
  36. color: #FFF;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="a">0</div>
  42. <div id="b">1</div>
  43. <div id="c">2</div>
  44. <div class="d">3</div>
  45. <div id="e">4</div>
  46. <div class="f">5</div>
  47. <p type="button" onclick="clicks()"> $('div[id]')(点我选择有ID属性的DIV) </p>
  48. </body>
  49. <script type="text/javascript">
  50. function clicks(){
  51. $('div[id]').addClass('addCss');
  52. }
  53. </script>
  54. </html>
Correcting teacher:西门大官人西门大官人

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post