Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> 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

Posty w temacie


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

 



RSS Aktualny czas: 22.08.2025 - 04:53