Witam, mam pewien problem, zrobiłem sobie tabelkę, która pa kliknięciu w pole zmienia kolor za każdym kliknięciem. Chcę, aby funkcja wykonała się tylko raz. Jak mogę to zrobić?
<script>
var tb1 = null;
//var g =Math.round(Math.random()*255);
//var b =Math.round(Math.random()*255);
function hookTable(){
tb1 = document.getElementById("main");
if ( tb1 !== null){
for (var i = 0; i < tb1.rows.length; i++){
for (var j=0; j < tb1.rows[i].cells.length;j++){
tb1.rows[i].cells[j].onclick = function() { getVal(this); };
}
}
}
}
function getVal(cel) {
//alert(cel.innerHTML);
var g =Math.round(Math.random()*255);
var b =Math.round(Math.random()*255);
cel.style.color="rgb(255,0,0)";
if (cel.innerHTML==="1"){
cel.style.backgroundColor="rgb(0,"+g+",0)";
return;
}else{
cel.style.backgroundColor="rgb(0,0,"+b+")";
}
}
</script>
<body onload="hookTable();">
<?php
$len = 10;
echo '<table id="main">';
for($i = 0; $i <$len; $i++){
echo "<tr>";
for ($j= 0; $j < $len; $j++){
echo '<td>'.mt_rand(0,1).'</td>';
}
echo "</tr>";
}
echo "</table>";
?>
</body>
Z góry dziękuję.