| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1021 人关注过本帖
标题:求助!function返回的对象是undefined
只看楼主 加入收藏
calfer
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-5-13
收藏
 问题点数:0 回复次数:1 
求助!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>[单击可在列表中显示]&nbsp;
            </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>
搜索更多相关主题的帖子: function undefined 对象 
2008-05-13 13:20
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:53
帖 子:1351
专家分:1200
注 册:2006-12-17
收藏
得分:0 
程序代码:
<html>
<head>
    <script type="text/javascript">
        var tempTable;
        function getTableObj(obj) {
            var tempObj = obj.parentElement;
            if("table" == tempObj.tagName.toLowerCase()) { 
                return tempObj;
            } else {
                return 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>

其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2008-05-13 20:29
快速回复:求助!function返回的对象是undefined
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.074408 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved