Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Skrocenie kodu js (petla for)
Volume
post
Post #1





Grupa: Zarejestrowani
Postów: 283
Pomógł: 11
Dołączył: 11.10.2004
Skąd: 3c

Ostrzeżenie: (0%)
-----


Witam
Mam taki o to kod ktory poprawnie dziala:

  1. <script type="text/javascript">
  2. var linki = document.getElementsByTagName('a');
  3. if (linki[0].className=='js_0')
  4. {
  5. linki[0].onclick = function() {return PokazAkapit('ad0');}
  6. }
  7. if (linki[1].className=='js_1')
  8. {
  9. linki[1].onclick = function() {return PokazAkapit('ad1');}
  10. }
  11. if (linki[2].className=='js_2')
  12. {
  13. linki[2].onclick = function() {return PokazAkapit('ad2');}
  14. }


Ale chcialbym go skrocic i przeksztalcic wiec probowalem petla for jak ponizej ale nie wiem czemu taki zapis jednak nie chce dzialac, czy moze ktos wie co tam moge zle robic?

  1. <script type="text/javascript">
  2. var linki = document.getElementsByTagName('a');
  3. for(i=0;i<3;i++)
  4. {
  5. if (linki[i].className=='js_'+i)
  6. {
  7. linki[i].onclick = function() {return PokazAkapit('ad'+i);}
  8. }
  9. }
Go to the top of the page
+Quote Post
erix
post
Post #2





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




Poczytaj, do czego służy klasa, a do czego identyfikator elementu, bo w tym tkwi Twój problem.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 2.10.2025 - 21:33