博主信息
博文 25
粉丝 0
评论 0
访问量 35686
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
jquery选择器
宿州市筋斗云信息科技-Vip
原创
1168人浏览过

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>
批改老师:西门大官人西门大官人

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学