Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427

    rimuovere elemento con Dom

    Con questa funzione creo degli elementi...

    function aggiungirid(rif,id){
    var tbody = document.getElementById(rif).getElementsByTagName( "TBODY")[0];
    var td1 = document.createElement("TD")
    var input = document.createElement("INPUT")
    td1.appendChild(input)

    row.appendChild(td1);
    tbody.appendChild(row);
    }

    come posso eliminare gli stessi elementi?

  2. #2
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427
    credo di essere su una buona strada...

    <script>
    function aggiungi(){
    var tbody = document.getElementById('extra').getElementsByTagN ame("TBODY")[0];
    var row = document.createElement("TR")
    var td1 = document.createElement("TD")
    td1.setAttribute("id","prova");
    td1.setAttribute("border","1");
    td1.setAttribute("bgcolor","#ffff00");
    td1.setAttribute("height","20");

    row.appendChild(td1);
    tbody.appendChild(row);
    }

    </script>
    <script>
    function rimuovi() {
    var d = document.getElementById('extra');
    var olddiv = document.getElementById('prova');
    d.removeChild(olddiv);
    }

    </script>
    <p onclick="aggiungi();" >
    nuova riga</p>

    <p onclick="rimuovi();" >
    rimuovi</p>


    <table border="1" width="100%" id="extra">
    <tbody>
    <tr>
    <td></td>
    </tr>
    </tbody>
    </table>

    errore di FF:

    Errore: uncaught exception: [Exception... "Node was not found" code: "8" nsresult: "0x80530008 (NS_ERROR_DOM_NOT_FOUND_ERR)" location: "http://localhost/intro_pwd.htm Line: 34"]

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    ti dice che il nodo non appartiene a quell' elemento, il che e' vero:
    perche' provi a rimuovere da document.getElementById('extra')
    qualcosa che e' stato appeso a
    document.getElementById('extra').getElementsByTagN ame("TBODY")[0]
    ?

  4. #4
    Utente di HTML.it L'avatar di agenti
    Registrato dal
    Feb 2002
    Messaggi
    2,427
    ho provato così...:


    <script>
    function rimuovi() {
    var d = document.getElementsByTagName("TBODY")[0];
    var olddiv = document.getElementById('prova');
    d.removeChild(olddiv);
    }

    </script>


    Errore: uncaught exception: [Exception... "Node was not found" code: "8" nsresult: "0x80530008 (NS_ERROR_DOM_NOT_FOUND_ERR)" location: "http://localhost/lastfinale4/intro_pwd.htm Line: 34"]

    ---------------

    ho provato anche così...:

    <script>
    function rimuovi() {
    var d = document.getElementById('extra').getElementsByTagN ame("TBODY")[0];
    var olddiv = document.getElementById('prova');
    d.removeChild(olddiv);
    }

    </script>

    Errore: uncaught exception: [Exception... "Node was not found" code: "8" nsresult: "0x80530008 (NS_ERROR_DOM_NOT_FOUND_ERR)" location: "http://localhost/lastfinale4/intro_pwd.htm Line: 34"]



  5. #5
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    anche qui non consideri correttamente l' alberatura:
    quello che ha id="prova" non e' figlio del primo tbody della tabella, ma figlio di un tr del primo tbody della tabella

    se per esempio si parlasse del secondo tr, il primo aggiunto dinamicamente, quello con indice 1 nell' array getElementsByTagName('tr')
    d dovrebbe essere uguale a
    var d = document.getElementById('extra').getElementsByTagN ame("TBODY")[0].getElementsByTagName('tr')[1];

    d.removeChild(olddiv);
    funzionerebbe


    x comodita', se tu volessi rimuovere la riga potresti basarti sul fatto che il parentNode della cella con id="prova" sara' sicuramente un <tr> e rimuovere quello
    ciao

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.