ugrás a tartalomhoz

egyszerre csak 1 fugvény????

grais · 2008. Jún. 26. (Cs), 00.35
sziasztok. a gondom az lenne, hogy szeretném, ha egy div elemet tudnak mozgatni mikozben fut benne egy javascript. kulon mukodnek a dolgok, de egyutt nem akar menni. Beteszem az eredeti forrastokat.

Sámláló egy div-ben lévő form mezőben.
  1. <html>  
  2. <head><title>Timeout Example</title>  
  3. <script language="javascript" type="text/javascript">  
  4. var counter = 0;  
  5. // call Update function in 2 seconds after first load  
  6. ID=window.setTimeout("Update();",1000);  
  7. function Update() {  
  8.    counter++;  
  9.    document.form1.input1.value="The counter is now at " + counter;  
  10. // set another timeout for the next count  
  11.    ID=window.setTimeout("Update();",20);  
  12. }  
  13. </script>  
  14. <script type="text/javascript" language="javascript" src="func.js"> </script>  
  15. <style>  
  16. #square {  
  17.         position:absolute;  
  18.         top:150;  
  19.         left:100;  
  20.         width:200;  
  21.         height:200;  
  22.         border:2px solid black;  
  23.         padding:10px;  
  24.         background-color:#00CC99;  
  25.         }  
  26. </style>  
  27.   
  28.   
  29.   
  30.   
  31. </head>  
  32. <body>  
  33. <h1>Timeout Example</h1>  
  34. <hr><p>  
  35. The text value below and the status line are being updated every two seconds.  
  36. Press the RESET button to restart the count, or the STOP button to stop it.  
  37. </p>  
  38. <p>  
  39.   
  40. </p>  
  41. <hr>  
  42.   
  43. <div id="square">  
  44.     
  45.   <h1>Timeout Example</h1>  
  46. <hr><p>  
  47. The text value below and the status line are being updated every two seconds.  
  48. Press the RESET button to restart the count, or the STOP button to stop it.  
  49. </p><hr>  
  50.     
  51.   <form NAME="form1">  
  52.     <input TYPE="text" NAME="input1" SIZE="25"><br>  
  53.     <input TYPE="button" VALUE="RESET" onClick="counter = 0;"><br>  
  54.     <input TYPE="button" VALUE="STOP" onClick="window.clearTimeout(ID);">  
  55.   </form>  
  56. </div>  
  57. <hr>  
  58. </body>  
  59. </html>  
A div mozgató rész fugvénye
  1. var x=100,y=150;  
  2.   
  3. function pos(dx,dy) {  
  4.     if (!document.getElementById) return;  
  5.     x += 10*dx;  
  6.     y += 10*dy;  
  7.     obj = document.getElementById("square");  
  8.     obj.style.top=y;  
  9.     obj.style.left=x;  
  10. }  
  11.   
  12. function hideSquare() {  
  13.     if (!document.getElementById) return;  
  14.     obj = document.getElementById("square");  
  15.     obj.style.display="none";  
  16. }  
  17.   
  18. function showSquare() {  
  19.     if (!document.getElementById) return;  
  20.     obj = document.getElementById("square");  
  21.     obj.style.display="block";  
  22. }  
Div mozgató rész:
  1. <html>  
  2. <head>  
  3. <title>Positioning Elements with JavaScript</title>  
  4. <script language="javascript" type="text/javascript"  
  5.     src="animate.js">  
  6. </script>  
  7. <style>  
  8. #square {  
  9.     position:absolute;  
  10.     top: 150;  
  11.     left: 100;  
  12.     width: 200;  
  13.     height: 200;  
  14.     border: 2px solid black;  
  15.     padding: 10px;  
  16.     background-color: #E0E0E0;  
  17. }  
  18. </style>  
  19. </head>  
  20. <body>  
  21. <h1>Positioning Elements</h1>  
  22. <hr>  
  23. <form name="form1">  
  24. <input type="button" name="left" value="<- Left"  
  25.     onClick="pos(-1,0);">  
  26. <input type="button" name="right" value="Right ->"  
  27.     onClick="pos(1,0);">  
  28. <input type="button" name="up" value="Up"  
  29.     onClick="pos(0,-1);">  
  30. <input type="button" name="down" value="Down"  
  31.     onClick="pos(0,1);">  
  32. <input type="button" name="hide" value="Hide"  
  33.     onClick="hideSquare();">  
  34. <input type="button" name="show" value="Show"  
  35.     onClick="showSquare();">  
  36. </form>  
  37. <hr>  
  38. <div id="square">  
  39. <p>This square is an absolutely positioned  
  40. layer that you can move using the buttons above.</p>  
  41. </div>  
  42. </body>  
  43. </html>  
 
1

Magyarán

grais · 2008. Jún. 26. (Cs), 00.41
Szóval a két forrást szeretném úgy egyesíteni, hogy a kov. eredményt kapjam. Van egy oldal amin van egy div, melyet az oldalon levo (left,right) gombokkal lehet mozgatni. A div tartalma egy mezo, amiben fusson a fenti kodban megtalalhato szamlalo resz. Es mindezt egyszerre. Addig jutottam, hogy megvan minden elem, fut a szamlalo de a vezerlo gombok meg nincsennek beteve. mihelyt bevagom maris nem hon be a szamlalo... :-(
mi lehet a gond?

Én pl igy is próbálkoztam:
  1. <html>  
  2. <head><title>Timeout Example</title>  
  3. <script language="javascript" type="text/javascript">  
  4. var counter = 0;  
  5. // call Update function in 2 seconds after first load  
  6. ID=window.setTimeout("Update();",1000);  
  7. function Update() {  
  8.    counter++;  
  9.    document.form1.input1.value="The counter is now at " + counter;  
  10. // set another timeout for the next count  
  11.    ID=window.setTimeout("Update();",20);  
  12. }  
  13. </script>  
  14. <script type="text/javascript" language="javascript" src="func.js"> </script>  
  15. <style>  
  16. #square {  
  17.         position:absolute;  
  18.         top:150;  
  19.         left:100;  
  20.         width:200;  
  21.         height:200;  
  22.         border:2px solid black;  
  23.         padding:10px;  
  24.         background-color:#00CC99;  
  25.         }  
  26. </style>  
  27.   
  28.   
  29.   
  30.   
  31. </head>  
  32. <body><script type="text/javascript" charset="utf-8" src="http://www.extra.hu/lib/webmaster/webmaster.js?scr=1&amp;username=grais&amp;host=grais.extra.hu&amp;rid=1508439475"></script>  
  33. <h1>Timeout Example</h1>  
  34. <hr><p>  
  35. The text value below and the status line are being updated every two seconds.  
  36. Press the RESET button to restart the count, or the STOP button to stop it.  
  37. </p>  
  38. <p>  
  39. <form name="form1">  
  40. <input type="button" name="left" value="<- left" onClick="pos(-1,0);">  
  41. <input type="button" name="right" value="right ->" onClick="pos(1,0);">  
  42. <input type="button" name="up" value="up" onClick="pos(0,-1);">  
  43. <input type="button" name="down" value="down" onClick="pos(0,1);">  
  44. <input type="button" name="hide" value="Hide" onClick="hideSquare();">  
  45. <input type="button" name="show" value="Show" onClick="showSquare();">  
  46. </form>  
  47. </p>  
  48. <hr>  
  49.   
  50. <div id="square">  
  51.     
  52.   <h1>Timeout Example</h1>  
  53. <hr><p>  
  54. The text value below and the status line are being updated every two seconds.  
  55. Press the RESET button to restart the count, or the STOP button to stop it.  
  56. </p><hr>  
  57.     
  58.   <form NAME="form1">  
  59.     <input TYPE="text" NAME="input1" SIZE="25"><br>  
  60.     <input TYPE="button" VALUE="RESET" onClick="counter = 0;"><br>  
  61.     <input TYPE="button" VALUE="STOP" onClick="window.clearTimeout(ID);">  
  62.   </form>  
  63. </div>  
  64. <hr>  
  65. </body>  
  66. </html>  
2

eligazodás

bandi · 2008. Jún. 26. (Cs), 02.40
Egy picit nehéz eligazodni a globális változók miatt, de most kb. látom, hogy hogyan kellene működnie. Elsőre mondjuk megpróbálnám, hogy a két formnak más legyen a neve, meg esetleg lefuttatnám egy validátorban is, a valid kódon könnyebben jósolható a működés. De persze ez nem biztos, hogy elég.

Még pár tipp, ha majd már megy a dolog:
  • felejtsd el a sok globális változót, próbálkozz pl. a module-patternnel
  • "eval is evil" -- a settimeout-nak meg lehet adni stringet is paraméternek, de nagyon rossz gyakorlat, mert rejtetten ez egy eval-t fog meghívni, add meg egyszerűen a függvény nevét
  • ez már talán inkább ízlés kérdése, de a document.form1.input1 helyett én mindig a standard DOM függvényeket használom (getElementById() és tsai)
3

getElementById()

grais · 2008. Jún. 26. (Cs), 15.20
Hát probálom ezt a getElementById() de szerintem valamivel nem vagyok tisztában.

obj = document.getElementById("input1");

aztán.

obj.value="The counter is now at " + counter;

gondolom igy kell a value hivatkozni az input1 beviteli mezonel. De nem megy... Valami mas gond lehet. A form1 et javitottam 2 re hogy ne legyen ismetlodes, de nem vitt elore. :-( ...

Illetve javitom a hozzaszolast. Most mukodik :-) .. igen a formokkal bekavartam... koszi. Viszont, ha segitenel hogy a par soral feljebbi irasomnal hogyan tudnam kivaltani a getElementById()vel a form2.input1.value hivatkozast megkoszonnem. Itt szerintem valami felreertesben elek. (meg csak most tanukgatok a scriptet :-) )
4

byID

zila · 2008. Jún. 26. (Cs), 15.29
Ahogy a neve mutatja egy dom elemet ad vissza ID szerint, te meg név (name) alapján akarod megkapni.
  1. <input type="text" id="akarmi" name="barmi" value="érték">  
  2. <script type="text/javascript">  
  3. obj=document.getElementById("akarmi");  
  4. alert(obj.value);  
  5. obj.value="új érték";  
  6. alert(obj.value);  
  7. </script>  
5

ID

vbence · 2008. Jún. 26. (Cs), 15.29
A getElementById használatához kell egy id attribútum is (a name nem elég).