Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript][HTML]Pomoc przy funkcji JS
Forum PHP.pl > Forum > Przedszkole
Raven1122
Witam, napisalem taki kodzik:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <script type="text/javascript">
  4. function togglediv(id){
  5. var divstatus = document.getElementById(id);
  6. if(divstatus.style.display == 'block')
  7. divstatus.style.display = 'none';
  8. else
  9. divstatus.style.display = 'block';
  10. }
  11. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  12. <meta http-equiv="Content-Language" content="pl" />
  13. <meta name="Robots" content="ALL" />
  14. <meta name="keywords" content="">
  15. <meta name="description" content="" />
  16. </head>
  17. <a onClick="togglediv('foo')">Naglowek 1</a>
  18. <div id="foo" style="display:block;">Tresc naglowka 1</div>
  19. <br /><br /><br />
  20. <a onClick="togglediv('foo2')">Naglowek 2</a>
  21. <div id="foo2" style="display:none;">Tresc naglowka 2</div>
  22. </body>
  23. </html>


I teraz potrzebuje zrobic tak, aby przy otworzeniu naglowka 2 automatycznie zamykal sie naglowek 1, jak to zrobic?
Crozin
W funkcji togglediv() najpierw ukryj wszystkie elementy, które mogą być odkryte, a następnie odkryj jeden element powiązany z klikniętym linkiem.
Raven1122
a w jaki sposob to zrobic? jestem w js jescze lajkiem wiec nie wiem jak wszystkim nadac display:none; biggrin.gif

@EDIT//

Ogolnie to nie chcialbym zeby kazdy naglowek mial inne id diva, bo jest to uciazliwe, czy jest to mozliwe takie cos?
b4rt3kk
Wydaje mi się, że jQuery w tej kwestii byłby bardziej odpowiedni w związku z prostotą użycia.

  1. $(
  2. $('a').click(togglediv());
  3. );
  4.  
  5. function togglediv() {
  6. $('a div').attr('style', 'display:none');
  7. $(this).children('div').show('fast');
  8. }


Stosując powyższy skrypt nie musisz używać ani id, ani onclick, wystarczy czysty HTML.
Raven1122
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <script type="text/javascript">
  4. $(
  5. $('a').click(togglediv());
  6. );
  7.  
  8. function togglediv() {
  9. $('a div').attr('style', 'display:none');
  10. $(this).children('div').show('fast');
  11. }
  12. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  13. <meta http-equiv="Content-Language" content="pl" />
  14. <meta name="Robots" content="ALL" />
  15. <meta name="keywords" content="">
  16. <meta name="description" content="" />
  17. </head>
  18. <a href="#">Naglowek 1</a>
  19. <div id="foo" style="display:block;">Tresc naglowka 1</div>
  20. <br /><br /><br />
  21. <a href="#">Naglowek 2</a>
  22. <div id="foo" style="display:block;">Tresc naglowka 2</div>
  23. </body>
  24. </html>

nie dziala
b4rt3kk
Chociaż nie, nie zauważyłem, że nie stosujesz struktury DOM, mała modyfikacja Twojego kodu HTML by pomogła. Np. dodaj <li> lub <div> zamykający oba elementy.

  1. <div>
  2. <a href="#">Naglowek 1</a>
  3. <div style="display:block;">Tresc naglowka 1</div>
  4. </div>
  5. <div>
  6. <a href="#">Naglowek 2</a>
  7. <div style="display:none;">Tresc naglowka 2</div>
  8. </div>


Teraz jQuery:

  1. $(
  2. $('a').click(togglediv());
  3. );
  4.  
  5. function togglediv() {
  6. $('div div').attr('style', 'display:none');
  7. $(this).closest('div').children('div').show('fast');
  8. }


W sekcji HEAD musisz dodać bibliotękę jQuery:
  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
Raven1122
Nie dziala :s
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <meta http-equiv="Content-Language" content="pl" />
  6. <meta name="Robots" content="ALL" />
  7. <meta name="keywords" content="">
  8. <meta name="description" content="" />
  9. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  10. <script type="text/javascript">
  11. $(
  12. $('a').click(togglediv());
  13. );
  14.  
  15. function togglediv() {
  16. $('div div').attr('style', 'display:none');
  17. $(this).closest('div').children('div').show('fast');
  18. }
  19. </head>
  20.  
  21. <div>
  22. <a href="#">Naglowek 1</a>
  23. <div style="display:block;">Tresc naglowka 1</div>
  24. </div>
  25. <div>
  26. <a href="#">Naglowek 2</a>
  27. <div style="display:none;">Tresc naglowka 2</div>
  28. </div>
  29. </body>
  30. </html>
b4rt3kk
Dobra, inaczej, to musi działać smile.gif

  1. <div>
  2. <a href="#">Naglowek 1</a>
  3. <div class="klasa1" style="display:block;">Tresc naglowka 1</div>
  4. </div>
  5. <div>
  6. <a href="#">Naglowek 2</a>
  7. <div class="klasa1" style="display:none;">Tresc naglowka 2</div>
  8. </div>


Skrypt:

  1. <script type="text/javascript">
  2. $(
  3. $('a').click(function(){
  4. $('.klasa1').attr('style', 'display: none;');
  5. $(this).closest('div').children('.klasa1').show('fast');
  6. };);
  7. );
Raven1122
dalej nie dziala
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.