javascript createElement的问题啊,大哥啊不是广告啊!
来源:百度知道 编辑:UC知道 时间:2024/06/08 10:01:30
<input type="button" onclick="makeTable()" value="make table"/><br />
<table width="500" height="300" id="table" class="calendar">
<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>
</table>
script如下
function change(tdObj){
tdObj.style.backgroundColor="#C1EDFF";
tdObj.style.fontSize="15px";
}
function rollBack(tdObj){
tdObj.style.backgroundColor="#FFFFFF";
tdObj.style.fontSize=null;
}
function makeTable(){
var curTable=document.getElementById("table");
curTable=curTable.lastChild;
if(curTable.childNodes.length<2){
for(var trcount=0;trcount<5;trcount++){
var tr=document.createElement("tr");
for(v
你设置的是td的背景颜色,应该是设置tr的背景颜色。
把makeTable方法里设置onmouseover/out方法改在tr 上
function makeTable(){
var curTable=document.getElementById("table");
curTable=curTable.lastChild;
if(curTable.childNodes.length<2){
for(var trcount=0;trcount<5;trcount++){
var tr=document.createElement("tr");
tr.onmouseover=function(){change(tr)};
tr.onmouseout=function(){rollBack(tr)};
for(var tdcount=0;tdcount<7;tdcount++){
var td=document.createElement("td");
td.id="td"+trcount.toString()+tdcount.toString();
td.innerText=tdcount;
tr.appendChild(td);
}
curTable.appendChild(tr);
}
}
}
td.onmouseover=function(){change(td)};
td.onmouseout=function(){rollBack(td)};
从效果来看应该是你元素得到的不对
你在change和rollback里面写两个alert看看是否正确的调用了
然后调整下试试td.onmouseout=rollBack(td);或者td.onmouseout=function(){rollBack(this)};