Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JS] problem z wywołaniem funkcji
robertas
post 25.01.2011, 21:20:42
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 25.01.2011

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


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
Go to the top of the page
+Quote Post
kamil4u
post 25.01.2011, 21:44:18
Post #2





Grupa: Zarejestrowani
Postów: 2 350
Pomógł: 512
Dołączył: 4.01.2009
Skąd: Wrocław / Świdnica

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


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

Ten post edytował kamil4u 25.01.2011, 21:47:36


--------------------
Go to the top of the page
+Quote Post
robertas
post 25.01.2011, 22:15:04
Post #3





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 25.01.2011

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


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
Go to the top of the page
+Quote Post
kamil4u
post 26.01.2011, 20:40:48
Post #4





Grupa: Zarejestrowani
Postów: 2 350
Pomógł: 512
Dołączył: 4.01.2009
Skąd: Wrocław / Świdnica

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


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>


--------------------
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 31.07.2025 - 04:13