Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    243

    js Creare Tabella con javascript

    Salve ragazzi,
    ho una tabella con id='tabella' composta da 1 riga e N colonne.
    Mi indicare una funzione javascript che mi permetta di aggiungere M righe alla tabella esistente ?
    Inoltre per ogni cella quindi per ogni TD della tabella dovrei poter inserire un valore,l'id la classe il cursor=hand e l'evento onchange='Calcola(this)'


    Grazie 1000

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    il resto lo fai tu... avevo sbattuto la testa contro l'aggiungere righe e celle alle tabelle qualche tempo fa... è la cosa è un po' artificiosa in IE, ti posto il codice di esempio a cui ero pervenuto.

    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    <!--
      function addrows(r) {
      	oldHTML = document.getElementById('tabella').getElementsByTagName('tbody')[0].innerHTML;
    	trs = document.getElementById('tabella').getElementsByTagName('tr').length;
    	tds = document.getElementById('tabella').getElementsByTagName('tr')[0].getElementsByTagName('td').length;
    	if (!document.all) {
    	  newHTML = "";
    	  for (var i = 0; i < r; i++) {
    	    newHTML += "<tr>";
    		for (var j = 0; j < tds; j++) {
    		  newHTML +="\n <td>"+(1+trs+i)*(j+1)+"</td>";
    		}
    		newHTML += "</tr>\n";
    		wholeHTML = oldHTML+newHTML;
    		document.getElementById('tabella').getElementsByTagName('tbody')[0].innerHTML = wholeHTML;
    	  }
    	}
    	else { //Era Internet Explorer
    	  for (var i=0; i < r; i++) {
    	  	var row;
    	    row = document.getElementById('tabella').insertRow();
    		for (var j=0; j < tds; j++) {
    		  var td;
    		  td = row.insertCell();
    		  td.innerHTML = (1+trs+i)*(1+j);
    		}
    	  }
    	}	
      }
    -->
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Tabella</title>
    </head>
    
    <body>
    <table id="tabella" style="width: 200px; border: 1px solid #000000;">
      <tr>
        <td>1</td>
    	<td>2</td>
    	<td>3</td>
      </tr>
      <tr>
        <td>2</td>
    	<td>4</td>
    	<td>6</td>
      </tr>
      <tr>
        <td>3</td>
    	<td>6</td>
    	<td>9</td>
      </tr>
    </table>
    
    Aggiungi righe:<input type="text" id="righe" value="Numero righe"><input type="button" value="Aggiungi righe" onClick="addrows(document.getElementById('righe').value);">
    </body>
    </html>
    Putroppo fa a pugni almeno con Opera (su IE e FF va).
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    non e' meglio usare i metodi del DOM?
    http://forum.html.it/forum/showthrea...hreadid=848168

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    penso di sì .... se solo lo avessi saputo
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

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 © 2024 vBulletin Solutions, Inc. All rights reserved.