首页 > 语言 > JavaScript > 正文

JQuery使用index方法获取Jquery对象数组下标的方法

2024-05-06 16:20:30
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了JQuery使用index方法获取Jquery对象数组下标的方法,涉及jQuery中index方法的使用技巧,需要的朋友可以参考下

本文实例讲述了JQuery使用index方法获取Jquery对象数组下标的方法。分享给大家供大家参考。具体实现方法如下:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  6. <title>index方法获取Jquery对象的数组下标</title> 
  7. <style type="text/css"> 
  8. #main{ 
  9. width:600px; 
  10. border:1px solid green; 
  11. margin:auto; 
  12. padding:10px; 
  13. } 
  14. #tbl{ 
  15. border-collapse:collapse; 
  16. border-top:1px solid red; 
  17. border-left:1px solid red; 
  18. margin:auto; 
  19. } 
  20. #tbl td{ 
  21. width:60px; 
  22. height:60px; 
  23. border-collapse:collapse; 
  24. border-bottom:1px solid red; 
  25. border-right:1px solid red; 
  26. } 
  27. </style> 
  28. <script src="jquery-1.6.2.min.js" type="text/javascript"></script> 
  29. <script type="text/javascript"> 
  30. var oldColor; 
  31. $(function () { 
  32. $("#tbl tr").hover( 
  33. function () { 
  34. oldColor = $(this).css("backgroundColor"); 
  35. $(this).css("backgroundColor", "yellow"); 
  36. }, 
  37. function () { 
  38. $(this).css("backgroundColor", oldColor); 
  39. }); 
  40. $("#tbl tr").click(function () { 
  41. alert($("#tbl tr").index($(this))); //获取元素下标 
  42. }); 
  43. }); 
  44. </script> 
  45. </head> 
  46. <body> 
  47. <div id="main"> 
  48. <table id="tbl"> 
  49. <tr> 
  50. <td>1</td><td>1</td><td>1</td> 
  51. </tr> 
  52. <tr> 
  53. <td class="tdClass">2</td><td>2</td><td>2</td> 
  54. </tr> 
  55. <tr> 
  56. <td>3</td><td>3</td><td>3</td> 
  57. </tr> 
  58. <tr> 
  59. <td>4</td><td>4</td><td class="tdClass">4</td> 
  60. </tr> 
  61. <tr> 
  62. <td>5</td><td>5</td><td>5</td> 
  63. </tr> 
  64. <tr> 
  65. <td>6</td><td>6</td><td class="tdClass">6</td> 
  66. </tr> 
  67. </table> 
  68. </div> 
  69. </body> 
  70. </html> 

希望本文所述对大家的jQuery程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选