Ciao a tutti, premetto che sono nuova in javascript... sto cercando di usare js ad oggetti, ed ho capito che è piuttosto diverso dagli altri linguaggi OOP. Questa è l'implementazione del mio oggetto:
codice:function tabella(){ var ultimo=0; var idmeno="meno"+ultimo; this.tabella=document.createElement('table'); this.riga=[]; this.rigatitolo=document.createElement('tr'); this.coltitolo=document.createElement('td'); this.coltitolo.setAttribute('colspan',3); this.riga[ultimo]=document.createElement('tr'); this.colspec=document.createElement('td'); this.colval=document.createElement('td'); this.colpiu=document.createElement('td'); this.titolo=document.createElement('input'); this.titolo.setAttribute('type','text'); this.titolo.setAttribute('class','titolo'); this.specifica=document.createElement('input'); this.specifica.setAttribute('type','text'); this.specifica.setAttribute('class','specifica'); this.valore=document.createElement('input'); this.valore.setAttribute('type','text'); this.valore.setAttribute('class','valore'); this.piu=document.createElement('div'); this.piu.setAttribute('class','piu'); this.piu.setAttribute('onclick',"this.aggiungispecifica()"); this.piu.innerHTML="+"; this.meno=document.createElement('div'); this.meno.setAttribute('onclick','this.rimuovispecifica()'); this.meno.setAttribute('class','meno'); this.meno.innerHTML="-"; this.meno.setAttribute('id',idmeno); this.creatabella=function () { document.getElementById('contenitore').appendChild(this.tabella); this.tabella.appendChild(this.rigatitolo); this.rigatitolo.appendChild(this.coltitolo); this.coltitolo.appendChild(this.titolo); this.tabella.appendChild(this.riga[0]); this.riga[0].appendChild(this.colspec); this.riga[0].appendChild(this.colval); this.riga[0].appendChild(this.colpiu); this.colspec.appendChild(this.specifica); this.colval.appendChild(this.valore); this.colpiu.appendChild(this.piu); ultimo++; } this.aggiungispecifica=function() { alert("ciao"); } }
E questa è la funzione dove creo l'oggetto che richiamo onload del body:
codice:function setta() { var tab=[]; tab[0]=new tabella(); tab[0].creatabella(); }
Questo è l'output:
esempio.jpg
Il problema: quando clicco il '+' dovrebbe invocare il metodo aggiungispecifica(), ma semplicemente non lo fa. Creando invece una funzione al di fuori dell'oggetto funziona. Qualcuno sa dirmi dove ho sbagliato? Grazie in anticipo![]()

Rispondi quotando