求助!function返回的对象是undefined
写了个隐藏显示列的功能,在用getTable方法得到table对象后返回,但在接这个对象的方法中却是undefined.不知是什么问题,请高手指点一下,谢谢!!<html>
<head>
<script type="text/javascript">
var tempTable;
function getTableObj(obj) {
var tempObj = obj.parentElement;
if("table" == tempObj.tagName.toLowerCase()) {
return tempObj;
} else {
getTableObj(tempObj);
}
}
function doHidden() {
//tempTable = event.srcElement.parentElement.parentElement;
tempTable = getTableObj(event.srcElement);
alert(tempTable);
var cellIndex = event.srcElement.cellIndex;
var rows = tempTable.rows.length;
for(var i = 0; i < rows; i++) {
var cellCount = 0;
for(var j = 0; j < tempTable.rows[i].cells.length; j++) {
if("none"==tempTable.rows[i].cells[j].style.display) {
continue;
} else if(cellCount == cellIndex) {
tempTable.rows[i].cells[j].style.display="none";
if(i == 0) {
var htext = tempTable.rows[i].cells[j].innerText;
var tt = document.createElement("label");
var tempDiv = document.getElementById("dispalyLabel");
tt.attachEvent("onclick",doShow);
tt.innerText = htext+" | ";
tt.id=j
tt.style.cursor='hand';
tt.title="显示该列";
tempDiv.appendChild(tt);
if("none" == document.all.hiddenField.style.display) {
document.all.hiddenField.style.display = "";
}
}
break;
} else {
cellCount++;
}
}
}
}
function doShow() {
var label=event.srcElement;
for(var i = 0; i < tempTable.rows.length; i++) {
tempTable.rows[i].cells[label.id].style.display="";
}
var tempDiv = document.all.dispalyLabel;
tempDiv.removeChild(label);
if(0 == document.all.dispalyLabel.childNodes.length) {
document.all.hiddenField.style.display = "none";
}
}
</script>
</head>
<body>
<form name='form1'>
<div id="hiddenField" style="width:100%;padding:5px;background-color:#FFF;display:none">
<fieldset class="new_fieldset">
<legend class="new_legend">
<font style="color:#124F98;">被隐藏的列</font>[单击可在列表中显示]
</legend>
<div id="dispalyLabel">
</div>
</fieldset>
</div>
<table border = '1' >
<tr onclick="doHidden();">
<td id='1' >1head</td>
<td id='2' >2head</td>
<td id='3' >3head</td>
<td id='4' >4head</td>
</tr>
<tr>
<td id='1'>111</td>
<td id='2'>222</td>
<td id='3'>333</td>
<td id='4'>444</td>
</tr>
<tr>
<td id='1'>111</td>
<td id='2'>222</td>
<td id='3'>333</td>
<td id='4'>444</td>
</tr>
<tr>
<td id='1'>111</td>
<td id='2'>222</td>
<td id='3'>333</td>
<td id='4'>444</td>
</tr>
</table>
</form>
</body>
</html>