Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript][jQuery] Rozwijanie i zwijanie elementów
Forum PHP.pl > Forum > Przedszkole
Puzi
Witam,

Oto mój HTML:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <link rel="stylesheet" href="./styles/style.css" type="text/css">
  5. <script type="text/javascript" src="./tools/jscripts/jquery-1.4.1.min.js"></script>
  6. <script type="text/javascript">
  7. $(document).ready(
  8. function()
  9. {
  10. $("#page_9w").click(
  11. function()
  12. {
  13. $("#page_9").toggle();
  14. }).toggle(function() { $("#img_9").attr('src','./images/minus.jpg'); }, function() { $("#img_9").attr('src','./images/plus.jpg'); });
  15.  
  16. $("#page_15w").click(
  17. function()
  18. {
  19. $("#page_15").toggle();
  20. }).toggle(function() { $("#img_15").attr('src','./images/minus.jpg'); }, function() { $("#img_15").attr('src','./images/plus.jpg'); });
  21.  
  22. $("#page_18w").click(
  23. function()
  24. {
  25. $("#page_18").toggle();
  26. }).toggle(function() { $("#img_18").attr('src','./images/minus.jpg'); }, function() { $("#img_18").attr('src','./images/plus.jpg'); });
  27. }
  28. );
  29.  
  30. </script>
  31. </head>
  32. <body>
  33. <table class="width_table" border="0" cellpadding="0" cellspacing="0">
  34. <tr>
  35. <td class="table_text_left">&nbsp;<a href="#" id="page_9w"><img src="./images/plus.jpg" id="img_9" alt=""></a>&nbsp;aaaaa</td>
  36. </tr>
  37. </table>
  38. <div id="page_9" style="display: none;">
  39. <table class="width_table" border="0" cellpadding="0" cellspacing="0">
  40. <tr>
  41. <td class="table_text_left" style="padding-left: 15px;">&nbsp;<a href="#" id="page_15w"><img src="./images/plus.jpg" id="img_15" alt=""></a>&nbsp;bbbbb</td>
  42. </tr>
  43. </table>
  44. <div id="page_15" style="display: none;">
  45. <table class="width_table" border="0" cellpadding="0" cellspacing="0">
  46. <tr>
  47. <td class="table_text_left" style="padding-left: 30px;">&nbsp;<a href="#" id="page_18w"><img src="./images/plus.jpg" id="img_18" alt=""></a>&nbsp;ccccc</td>
  48. </tr>
  49. </table>
  50. <div id="page_18" style="display: none;">
  51. <table class="width_table" border="0" cellpadding="0" cellspacing="0">
  52. <tr>
  53. <td class="table_text_left" style="padding-left: 45px;">&nbsp;<img src="./images/empty.jpg" id="img_19" alt="">&nbsp;ddddd</td>
  54. </tr>
  55. </table>
  56. </div>
  57. </div>
  58. <table class="width_table" border="0" cellpadding="0" cellspacing="0">
  59. <tr>
  60. <td class="table_text_left" style="padding-left: 15px;">&nbsp;<img src="./images/empty.jpg" alt="">&nbsp;eeeee</td>
  61. </tr>
  62. </table>
  63. </div>
  64. <table class="width_table" border="0" cellpadding="0" cellspacing="0">
  65. <tr>
  66. <td class="table_text_left">&nbsp;<img src="./images/empty.jpg" alt="">&nbsp;fffff</td>
  67. </tr>
  68. </table>
  69. <table class="width_table" border="0" cellpadding="0" cellspacing="0">
  70. <tr>
  71. <td class="table_text_left">&nbsp;<img src="./images/empty.jpg" alt="">&nbsp;ggggg</td>
  72. </tr>
  73. </table>
  74. </body>
  75. </html>


I mam pytanie odnośnie tego fragmentu kodu:
  1. <script type="text/javascript">
  2. $(document).ready(
  3. function()
  4. {
  5. $("#page_9w").click(
  6. function()
  7. {
  8. $("#page_9").toggle();
  9. }).toggle(function() { $("#img_9").attr('src','./images/minus.jpg'); }, function() { $("#img_9").attr('src','./images/plus.jpg'); });
  10.  
  11. $("#page_15w").click(
  12. function()
  13. {
  14. $("#page_15").toggle();
  15. }).toggle(function() { $("#img_15").attr('src','./images/minus.jpg'); }, function() { $("#img_15").attr('src','./images/plus.jpg'); });
  16.  
  17. $("#page_18w").click(
  18. function()
  19. {
  20. $("#page_18").toggle();
  21. }).toggle(function() { $("#img_18").attr('src','./images/minus.jpg'); }, function() { $("#img_18").attr('src','./images/plus.jpg'); });
  22. }
  23. );
  24.  
  25. </script>


Czy da się zrobić za pomocą jednej funkcji aby rozwijała i ukrywała dany element po ID??
Bo teraz muszę definiować każde kliknięcie dla danego elementu a to jest trochę męczące jak jest tego dużo.

Pozdrawiam i proszę o pomoc.
skowron-line
Sprawdź sobie coś takiego http://www.i-marco.nl/weblog/jquery-accordion-menu/
albo w guuglu wpisz "jquery accordion"
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.