DOM setattribute
Hvorfor virker det her ikke?Jeg vil gerne sætte baggrundsfarven for en celle i en tabel ved hjælp af DOM-Javascriptet.
På forhånd tak.
<html>
<head>
<title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script>
function start() {
mybody=document.getElementsByTagName("body").item(0);
mytable=mybody.getElementsByTagName("table").item(0);
mytablebody=mytable.getElementsByTagName("tbody").item(0);
myrow=mytablebody.getElementsByTagName("tr").item(0);
mycel=myrow.getElementsByTagName("td").item(0);
mycel.setAttribute("width","100");
mycel.setAttribute("align","right");
mycel.setAttribute("bgcolor","red");
myceltext=mycel.childNodes.item(0);
currenttext=document.createTextNode(myceltext.data);
mybody.appendChild(currenttext);
}
</script>
</head>
<body onload="start()">
<table border="1">
<tbody>
<tr>
<td bgcolor="green">celle 1</td><td>celle 2</td>
</tr>
<tr>
<td>celle 3</td><td bgcolor="#0000FF">celle 4</td>
</tr>
</tbody>
</table>
</body>
</html>