Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JS] Klasa i getElementById, getElementById wywolany w klasie zwraca undefined
comanderv
post
Post #1





Grupa: Zarejestrowani
Postów: 87
Pomógł: 1
Dołączył: 2.12.2008

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


witam,
piszę sobie małą klasę w której potrzebuje odwołać się do istniejącego elementu HTML, metoda getElementById nie skutkuje...
wygląda to mniej więcej tak:
  1. function klasa()
  2. {
  3. this.x=parseInt(Math.random()*300);
  4. this.y=parseInt(Math.random()*100);
  5.  
  6. //pare innych pierdół
  7.  
  8. var dzieci = document.getElementById('div2').children;
  9. this.d = dzieci.length;
  10. }
  11.  
  12. //wywolanie alert(this.d) drukuje mi 'undefined'


w pisaniu obiektowym a już tym bardziej w JSie jestem dość raczkujący, prosiłbym o pomoc (IMG:style_emoticons/default/smile.gif)

Ten post edytował comanderv 22.02.2011, 11:37:59
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
comanderv
post
Post #2





Grupa: Zarejestrowani
Postów: 87
Pomógł: 1
Dołączył: 2.12.2008

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


caly kod JS zapisany jest zaraz przed zamkniecie elementu body,
do tego wywolanie mam w $(window).load zeby sie zdarzylo wszystko przed wywolanie zaladowac...
klasa nie widzi wgl ze jest w jakims documencie... ale zeby smieszniej bylo:
  1. function klasa()
  2. {
  3. this.x=parseInt(Math.random()*300);
  4. this.y=parseInt(Math.random()*100);
  5.  
  6. var woda = document.getElementById('woda');
  7. var ryby = woda.getElementsByTagName("img");
  8. var imgTag = document.createElement("img");
  9. imgTag.src='ryba.png';
  10. imgTag.setAttribute("position","top","left");
  11. imgTag.style.position='absolute';
  12. imgTag.style.top=this.y+'px';
  13. imgTag.style.left=this.x+'px';
  14. imgTag.className ="ryba";
  15.  
  16. this.p = ryby[0];
  17. // this.p = document.location; tez undefined
  18.  
  19. woda.appendChild(imgTag);
  20. }

nowego img tworzy, wewnatrz zlapanego przez getElementById diva, dalej juz lezy..
gdzie jest jakis blad?

Ten post edytował comanderv 22.02.2011, 11:43:14
Go to the top of the page
+Quote Post

Posty w temacie


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: 11.10.2025 - 13:56