Autor | Zpráva | ||
---|---|---|---|
user243 Profil |
#1 · Zasláno: 23. 6. 2013, 15:43:39
Dobrý den,
šlo by nějakým způsobem následující zápis zkrátit/zjednodušit? var divy = document.getElementById("input-file").getElementsByTagName("div"); var inputy = document.getElementById("input-file").getElementsByTagName("input"); inputy[0].onchange = function() {divy[1].className = "";}; inputy[1].onclick = function() {inputy[0].value = "";}; inputy[2].onchange = function() {divy[2].className = "";}; inputy[3].onclick = function() {inputy[2].value = "";}; inputy[4].onchange = function() {divy[3].className = "";}; inputy[5].onclick = function() {inputy[4].value = "";}; inputy[6].onchange = function() {divy[4].className = "";}; inputy[7].onclick = function() {inputy[6].value = "";}; inputy[8].onchange = function() {divy[5].className = "";}; inputy[9].onclick = function() {inputy[8].value = "";}; inputy[10].onchange = function() {divy[6].className = "";}; inputy[11].onclick = function() {inputy[10].value = "";}; inputy[12].onchange = function() {divy[7].className = "";}; inputy[13].onclick = function() {inputy[12].value = "";}; inputy[14].onchange = function() {divy[8].className = "";}; inputy[15].onclick = function() {inputy[14].value = "";}; inputy[16].onchange = function() {divy[9].className = "";}; inputy[17].onclick = function() {inputy[16].value = "";}; <div id="input-file"> <div> <label>Screenshot: <input type="file" name="soubor[0]" accept="image/jpeg"></label><input type="button" value="Odebrat"> </div> <div class="skryto"> <label>Screenshot: <input type="file" name="soubor[1]" accept="image/jpeg"></label><input type="button" value="Odebrat"> </div> <div class="skryto"> <label>Screenshot: <input type="file" name="soubor[2]" accept="image/jpeg"></label><input type="button" value="Odebrat"> </div> <div class="skryto"> <label>Screenshot: <input type="file" name="soubor[3]" accept="image/jpeg"></label><input type="button" value="Odebrat"> </div> <div class="skryto"> <label>Screenshot: <input type="file" name="soubor[4]" accept="image/jpeg"></label><input type="button" value="Odebrat"> </div> <div class="skryto"> <label>Screenshot: <input type="file" name="soubor[5]" accept="image/jpeg"></label><input type="button" value="Odebrat"> </div> <div class="skryto"> <label>Screenshotr: <input type="file" name="soubor[6]" accept="image/jpeg"></label><input type="button" value="Odebrat"> </div> <div class="skryto"> <label>Screenshot: <input type="file" name="soubor[7]" accept="image/jpeg"></label><input type="button" value="Odebrat"> </div> <div class="skryto"> <label>Screenshot: <input type="file" name="soubor[8]" accept="image/jpeg"></label><input type="button" value="Odebrat"> </div> <div class="skryto"> <label>Screenshot: <input type="file" name="soubor[9]" accept="image/jpeg"></label><input type="button" value="Odebrat"> <p> Můžete nahrát nejvíce 10 souboru najednou.</p> </div> </div> díky. |
||
Tori Profil |
#2 · Zasláno: 23. 6. 2013, 16:29:44
Nešlo by využít probublávání? Pověsit handler onclick na nadřazený prvek, zjistit jestli se kliknulo na <input>/<label> a podle toho s něčím provést nějakou akci. Ale nestudovala jsem detailně ten kód, je pro mě dost matoucí způsob, jak se klikne na prvek a pracuje s jeho (více nebo méně vzdáleným) sousedem.
|
||
pako Profil * |
#3 · Zasláno: 23. 6. 2013, 20:57:13
obsluhu udalosti zaves na formular/div, v ktorom mas inputy a tak ako hovori [#2] Tori - sleduj ciel udalosti
event.target /event.srcElement
potom porovnaj v cykle ten ciel s kolekciou input ov a tak zistis, na ktorom z nich doslo k udalosti a s tym mozes dalej pracovat podla potreby
|
||
user243 Profil |
#4 · Zasláno: 24. 6. 2013, 08:26:55
moc jsem to nepochopil s tím event.target/event.srcElement;
zkusil jsem tedy provést odposlouchávání a výsledek byl stejný - nefunkční var j = 1; for (var i = 0; i < (inputy.length - 1); i++) { inputy[i].addEventListener("change", function(){if (divy[j] && inputy[i].value != "") {divy[j].className = ""; j++;};}, false); i++; inputy[i].addEventListener("click", function(){inputy[(i - 1)].value = "";}, false); } i konečnou hodnotu; stačilo by mi, kdyby si ta anonymní funkce nějak zapamatovala její hodnotu při provádění scriptu;
|
||
_es Profil |
#5 · Zasláno: 24. 6. 2013, 09:39:08
user243:
Metódu addEventListener niektoré prehliadače nepodporujú. Použi jednoduché onclick a onchange .
„moc jsem to nepochopil s tím event.target/event.srcElement“ Mysleli niečo podobné tomuto. |
||
user243 Profil |
#6 · Zasláno: 26. 6. 2013, 15:00:31
aha, takže zůstanu u toho svého;
|
||
Časová prodleva: 11 let
|
0