合并单元格 应该通过 rowSpan colSpan实现

解决方案 »

  1.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    var Row_Count="",Cell_Count="",TableName;
    TableName = "table";    function ClearPanel()
        {
            document.getElementById("Row").disabled=false;
            document.getElementById("Cell").disabled=false;
            document.getElementById("Row").value="";
            document.getElementById("Cell").value="";
            document.getElementById("left").innerHTML="<br>";
        }
        
        function CreatTable()
        {
            if(document.getElementById("Row").value==""||document.getElementById("Cell").value=="")
            {
                alert("行列不可以为空!");
            }
            else
            {
                document.getElementById("Row").disabled="disabled";
                document.getElementById("Cell").disabled="disabled";
                var CT = new  Table()
                CT.id = TableName;
                CT.Row = document.getElementById("Row").value;
                CT.Cell = document.getElementById("Cell").value;
                CT.Border = "1";
                CT.BorderColor = "#000000";
                CT.CellSpacing = "0";
                CT.Width = "40px";
                CT.Height = "20px";
                CT.OnClick = "";
                document.getElementById("left").innerHTML="<br>"+CT.OutPut();
            }
        }
        
        function Table()  
        {
            this.id = "";
            this.Row = "";
            this.Cell = "";
            this.Border = "";
            this.BorderColor = "";
            this.CellSpacing = "";
            this.Width = "";
            this.Height = "";
            this.OnClick = "";
            
            this.OutPut = function ()
            {
                var TB="",Tr="",Td="";
                
                for(var i=0;i<this.Row;i++)
                {
                    for(var j=0;j<this.Cell;j++)
                    {
                        Td+="<TD id='"+i+j+"' width='"+this.Width+"' height='"+this.Height+"' colspan='1' >"+i+j+"</TD>";
                    }
                    Tr += "<TR id='"+i+"' onmousedown='SelectTr(this);'>"+Td+"</TR>";
                    Td="";
                }
                TB="<Table id='"+this.id+"' border='"+this.Border+"' bordercolor='"+this.BorderColor+"' cellspacing='"+this.CellSpacing+"' >"+Tr+"</Table>";
                return TB;
            }
        }
        
        function SelectTr(TR)
        {
            Row_Count = TR.rowIndex;
            ClearColor(Row_Count);
            document.getElementById("DelRow").disabled = (Row_Count==null || Row_Count<0);
            document.getElementById("UniteRow").disabled =(Row_Count==null || Row_Count<0);
        }    function ClearColor(n)
        {
            var objTable=document.getElementById(TableName);
            for(var i=0;i<objTable.rows.length;i++)
            {
                if(i==n)
                {
                    objTable.rows[i].style.backgroundColor="#00FFFF";
                }
                else
                {
                    objTable.rows[i].style.backgroundColor="#FFFFFF";
                }
            }
        }
        
        
        
        function DelRow()
        {
            var objTable=document.getElementById(TableName);
            objTable.deleteRow(Row_Count);
            document.getElementById("DelRow").disabled = "disabled";
            document.getElementById("UniteRow").disabled ="disabled";
        }
        
        function UniteRow()
        {
            var objTable=document.getElementById(TableName);
            var content="";
    var len = objTable.rows[Row_Count].cells.length;
            for(var j=0;j<len;j++)
            {
                content+=objTable.rows[Row_Count].cells[j].innerHTML;
            }
            document.getElementById(Row_Count+""+(len-1)).innerHTML=content;
            for(var j=0;j<len-1;j++)
            {
                document.getElementById(Row_Count+""+j).removeNode(true);
                
            }
            document.getElementById(Row_Count+""+(len-1)).colspan=len;
    document.getElementById(Row_Count+""+(len-1)).width='';
            document.getElementById(Row_Count+""+(len-1)).style.backgroundColor="yellow";        'alert(document.getElementById(Row_Count+""+(len-1)).parentElement.outerHTML);
    document.getElementById("left").innerHTML = document.getElementById("left").innerHTML;        document.getElementById("DelRow").disabled = "disabled";
            document.getElementById("UniteRow").disabled ="disabled";
        }    
        function regInput(obj, reg, inputStr)
        {
            var docSel    = document.selection.createRange()
            if (docSel.parentElement().tagName != "INPUT")    return false
            oSel = docSel.duplicate()
            oSel.text = ""
            var srcRange    = obj.createTextRange()
            oSel.setEndPoint("StartToStart", srcRange)
            var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
            return reg.test(str)
        }
    </script></head><body> 
    <div id="container">    <div id="top">
        </div>
        
        <div id="left">
            <br />
        </div>
        
        <div id="right">
            <ul>
                <br>
                <br>行数:<input type="text" id="Row" size="5" onkeypress = "return regInput(this,/^[0-9]*$/,String.fromCharCode(event.keyCode))" onpaste = "return regInput(this,/^[0-9]*$/,window.clipboardData.getData('Text'))" ondrop = "return regInput(this,/^[0-9]*$/,event.dataTransfer.getData('Text'))" style="ime-mode:Disabled" />
                    列数:<input type="text" id="Cell" size="5" onkeypress = "return regInput(this,/^[0-9]*$/,String.fromCharCode(event.keyCode))" onpaste = "return regInput(this,/^[0-9]*$/,window.clipboardData.getData('Text'))" ondrop = "return regInput(this,/^[0-9]*$/,event.dataTransfer.getData('Text'))" style="ime-mode:Disabled" />
                <br><input type="submit" id="ClearPanel" value="重置容器" onclick="ClearPanel();" />
                    <input type="submit" id="CreatTable" value="创建表格" onclick="CreatTable();" />
                <br><input type="submit" id="DelRow" value="删除行" onclick="DelRow();" disabled="disabled" />
                    <input type="submit" id="UniteRow" value="合并单元格" onclick="UniteRow();" disabled="disabled" />
                <br>
                <br>创建好表格后,请点击表格选择好行,再进行删除,合并!
            </ul>
      </div>
        
    </div>
    </body>
    </html>
      

  2.   

            //alert(document.getElementById(Row_Count+""+(len-1)).parentElement.outerHTML);
            document.getElementById("left").innerHTML = document.getElementById("left").innerHTML;
    我也感觉奇怪  加了上面这句才好