dany84
17-03-2009, 13:49
Ciao a tutti,
sto cercando di creare una tabella dinamicamente con Javascript.
La tabella deve venire creata quando si clicca su un bottone presente nella pagina.
Su Firefox funziona alla perfezione, su IE non si vede nulla, non dà neanche qualche errore javascript, ci si clicca sopra e non fa assolutamente nulla.
Vagando per la rete ho trovato vari esempi, tra cui uno che faceva al caso mio e su IE funziona. Questo esempio fa però eseguire la funzione javascript sull'onLoad del <body>. Modificandolo e facendo eseguire la funzione sull'onClick di un bottone, non funziona più.
Quindi chiedo a voi, com'è possibile che la stessa funzione javascript funzioni sull'onLoad e non sull'onClick?
Tutto questo solo su IE.
Vi posto il codice dell'esempio che ho trovato in rete e che ho modificato, che è meno incasinato del mio originale:
<html>
<head>
<title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script>
function start() {
// get the reference for the body
var body = document.getElementsByTagName("body")[0];
// creates a <table> element and a <tbody> element
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
// creating all cells
for (var j = 0; j < 2; j++) {
// creates a table row
var row = document.createElement("tr");
for (var i = 0; i < 2; i++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the table row
var cell = document.createElement("td");
var cellText = document.createElement("input");
cellText.setAttribute("type","button");
cell.appendChild(cellText);
row.appendChild(cell);
}
// add the row to the end of the table body
tblBody.appendChild(row);
}
// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// appends <table> into <body>
body.appendChild(tbl);
// sets the border attribute of tbl to 2;
tbl.setAttribute("border", "2");
}
</script>
</head>
<body>
<p>
<form name="myForm">
<input type="button" value="func" onclick="start()">
</form>
</p>
</body>
</html>
sto cercando di creare una tabella dinamicamente con Javascript.
La tabella deve venire creata quando si clicca su un bottone presente nella pagina.
Su Firefox funziona alla perfezione, su IE non si vede nulla, non dà neanche qualche errore javascript, ci si clicca sopra e non fa assolutamente nulla.
Vagando per la rete ho trovato vari esempi, tra cui uno che faceva al caso mio e su IE funziona. Questo esempio fa però eseguire la funzione javascript sull'onLoad del <body>. Modificandolo e facendo eseguire la funzione sull'onClick di un bottone, non funziona più.
Quindi chiedo a voi, com'è possibile che la stessa funzione javascript funzioni sull'onLoad e non sull'onClick?
Tutto questo solo su IE.
Vi posto il codice dell'esempio che ho trovato in rete e che ho modificato, che è meno incasinato del mio originale:
<html>
<head>
<title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script>
function start() {
// get the reference for the body
var body = document.getElementsByTagName("body")[0];
// creates a <table> element and a <tbody> element
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
// creating all cells
for (var j = 0; j < 2; j++) {
// creates a table row
var row = document.createElement("tr");
for (var i = 0; i < 2; i++) {
// Create a <td> element and a text node, make the text
// node the contents of the <td>, and put the <td> at
// the end of the table row
var cell = document.createElement("td");
var cellText = document.createElement("input");
cellText.setAttribute("type","button");
cell.appendChild(cellText);
row.appendChild(cell);
}
// add the row to the end of the table body
tblBody.appendChild(row);
}
// put the <tbody> in the <table>
tbl.appendChild(tblBody);
// appends <table> into <body>
body.appendChild(tbl);
// sets the border attribute of tbl to 2;
tbl.setAttribute("border", "2");
}
</script>
</head>
<body>
<p>
<form name="myForm">
<input type="button" value="func" onclick="start()">
</form>
</p>
</body>
</html>