Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JS] problem z wywołaniem funkcji
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
robertas
mam skrypt:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Przykładowy dokument</title>
  6.  
  7. <style type="text/css">
  8. #abc{
  9. background: red;
  10. height: 300px;
  11. }
  12. </style>
  13. <script type="text/javascript">
  14.  
  15. var c = 0;
  16.  
  17. document.addEventListener("DOMContentLoaded", function(){
  18. showtime = setInterval("animate()", 2);
  19. }, false);
  20.  
  21. function animate(){
  22. var abc = document.getElementById("abc");
  23. var h = abc.offsetHeight;
  24.  
  25. if(c == 700){
  26. clearInterval(showtime);
  27. }
  28.  
  29. h++;
  30. c++;
  31. abc.style.height = h + "px";
  32. }
  33.  
  34. </script>
  35. </head>
  36. <body>
  37. <div id="abc">
  38. Lorem ipsum dol sir amdet
  39.  
  40. </div>
  41. </body>
  42. </html>


objąłem ten skrypt function i dodałem ONclick dla jej wywołania

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Przykładowy dokument</title>
  6.  
  7. <style type="text/css">
  8. #abc{
  9. background: red;
  10. height: 300px;
  11. }
  12. </style>
  13. <script type="text/javascript">
  14. function zmien()
  15. {
  16. var c = 0;
  17.  
  18. document.addEventListener("DOMContentLoaded", function(){
  19. showtime = setInterval("animate()", 2);
  20. }, false);
  21.  
  22. function animate(){
  23. var abc = document.getElementById("abc");
  24. var h = abc.offsetHeight;
  25.  
  26. if(c == 700){
  27. clearInterval(showtime);
  28. }
  29.  
  30. h++;
  31. c++;
  32. abc.style.height = h + "px";
  33. }
  34. }
  35. </script>
  36. </head>
  37. <body>
  38. <div id="abc">
  39. Lorem ipsum dol sir amdet
  40. <p onclick="zmien();">Kliknij aby rozwinac</p>
  41. </div>
  42. </body>
  43. </html>


niestety wtedy skrypt nie działa. Proszę o pomoc, siedzę 2 dni i nic nie przychodzi mi do głowy dlaczego w ten sposób nie mogę włączać tej funkcji
kamil4u
Wiesz co robi ta linia:
Cytat
document.addEventListener("DOMContentLoaded", function(){
?
Bo z tego co piszesz wynika, że nie...

Z całej tej instrukcji zostaw, tylko:
Kod
showtime = setInterval("animate()", 2);



I <p> nie jest dobrym znacznikiem do tego... Użyj <span> lub coś podobnego, po 3 słowa rzadko tworzą akapit smile.gif
robertas
ok zmieniłem teraz.

  1. <script type="text/javascript">
  2. function zmien() {
  3.  
  4.  
  5.  
  6.  
  7. showtime = setInterval("animate()", 2);
  8.  
  9. }
  10. function animate(){
  11. var abc = document.getElementById("abc");
  12. var h = abc.offsetHeight;
  13. var c = 0;
  14. if(c == 700){
  15. clearInterval(showtime);
  16. }
  17.  
  18. h++;
  19. c++;
  20. abc.style.height = h + "px";
  21.  
  22.  
  23.  
  24. }


z tym że clearinterval już nie zatrzymuje
kamil4u
Jak nie działa jak działa smile.gif
Kod
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Przykładowy dokument</title>

            <style type="text/css">
                #abc{
                    background: red;
                    height: 300px;
                }
            </style>
            <script type="text/javascript">
var showtime;
            function zmien()
            {
                var c = 0;
                showtime = setInterval(animate, 2);

                function animate(){
                    var abc = document.getElementById("abc");
                    var h   = abc.offsetHeight;

                    if(c == 700){
                        clearInterval(showtime);
                    }

                    h++;
                    c++;
                    abc.style.height = h + "px";
                }
            }
            </script>
        </head>
        <body>
            <div id="abc">
            Lorem ipsum dol sir amdet<br>
            <span onclick="zmien();">Kliknij aby rozwinac</span>
    </div>
    </body>
    </html>
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.