Sasa Posted February 6, 2008 Report Share Posted February 6, 2008 Šīs funkcija izveido man jaunu elementu: <script type="text/javascript"> function addElement() { var lyr = document.getElementById('input_area'); //layer to create under var newEl = document.createElement('input'); //element type var newEl_1 = document.createElement('<br>'); //element type <br> newEl.setAttribute('id','z'); newEl.setAttribute('name','attach2') newEl.setAttribute('type','file') newEl.setAttribute('size','35') newEl.setAttribute('style','padding:2px') newEl.setAttribute('onchange=','"addElement()"') lyr.appendChild(newEl); lyr.appendChild(newEl_1); } </script> tiklīdz kā ir noticis notikums: <input name="attach1" type="file" size=35 onchange="addElement()" > <br> Bet ir viena lieta es jaunizveidotajā input elementu nevaru izveidot onchange eventu newEl.setAttribute('onchange=','"addElement()"') <- šītā neņem pretī. Un jauna'jiem input elementiem vajag newEl.setAttribute('name','attach2') katru reizi name vērtību mainīt no attach2 uz attach3 un tā tālāk (ja būs 10 inputi tad name jābūt no attach1 līdz attach10 ). šobrīd man katru reizi pievienojas inputs ar name attach2. Link to comment Share on other sites More sharing options...
Vebers Posted February 6, 2008 Report Share Posted February 6, 2008 (edited) newEl.setAttribute('onchange=','"addElement()"') vietā dari tā: newEl.onchange= function () { addElement(); } tad darbosies arī uz IE. un par attach1, attach2 - uztaisi globālu mainīgo kuru pēc katra jauna elementa izveides palielini par 1. Edited February 6, 2008 by Vebers Link to comment Share on other sites More sharing options...
Sasa Posted February 6, 2008 Author Report Share Posted February 6, 2008 kā pārbaudīt vai izveidots jauns elements? Link to comment Share on other sites More sharing options...
Vebers Posted February 6, 2008 Report Share Posted February 6, 2008 To jau tu vienreiz prasīji if(document.getElementById('id')) { exists } else { doesn`t exist } Bet tavā variantā: if(var newEl = document.createElement('input')) { created } else { failed to create } Link to comment Share on other sites More sharing options...
Sasa Posted February 6, 2008 Author Report Share Posted February 6, 2008 pārbaude: var i =1; if (var newEl = document.createElement('input')) { alert( i + 1); } bet pie i visu laiku pieskaitās 1 un visu laiku ir 2. kā man katru reizi kā tiek pievienots elements palielināt i par 1? Link to comment Share on other sites More sharing options...
Vebers Posted February 6, 2008 Report Share Posted February 6, 2008 i = 1; if (var newEl = document.createElement('input')) { alert( i); i++; } Link to comment Share on other sites More sharing options...
andrisp Posted February 6, 2008 Report Share Posted February 6, 2008 īstenībā nevaru iedomāties situāciju, kad document.createElement('input') varētu atgriezt false. Var gan gadīties, ka šausmīgi vecs pārlūks ar šausmīgi vecu JS implementāciju, kur neeksistē attiecīgā metode, bet tad pārbaude jāveic šādi: if (document.createElement) { // viss ok } Link to comment Share on other sites More sharing options...
Sasa Posted February 6, 2008 Author Report Share Posted February 6, 2008 hmm ... tā pat negrib skaitīt es to pārbaudi iemetu tanī pašā funkcijā kur man ir tā jaunā elementa pievienošana! Link to comment Share on other sites More sharing options...
Vebers Posted February 6, 2008 Report Share Posted February 6, 2008 (edited) To i = 1; iznem ārā no f-jas. un palielini viņu tikai f-jā! Imho, tas tač loģiski, jo pašlaik katru reizi izsaucot f-ju tu nodefinē ka i = 1! Edited February 6, 2008 by Vebers Link to comment Share on other sites More sharing options...
Sasa Posted February 6, 2008 Author Report Share Posted February 6, 2008 (edited) man IE negrib ņemt pretī setAttribute style: newEl.setAttribute('style','BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; Z-INDEX: 1; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid') un arī šitā: newEl.style.setAttribute('cssText', 'BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid;', 0); Izlaboju neliekot iekš setAttribute to style, bet ieliku css failā pēc id! Edited February 6, 2008 by Sasa Link to comment Share on other sites More sharing options...
andrisp Posted February 6, 2008 Report Share Posted February 6, 2008 el.style.border = '1px solid black'; el.style.zIndex = '1'; Link to comment Share on other sites More sharing options...
Recommended Posts