日期:2014-05-19  浏览次数:20526 次

一个Javascript复制对象问题
大家好!我现在要处理在Table中

某行任一控件获得焦点该行即变色,失去焦点则恢复原色

的问题,初步想法是用一个全局变量保存变色前的行对象,代码如下:


////////////////////////////////////////////////////////////////////
var   DBGridPreFocusRow;//全局变量,保存行

function   ChangeCurrentRowColor(o,RowColor,TextColor)
{    
        var   CurrentCell   =   o.parentNode;
        var   CurrentRow   =   o.parentNode.parentNode;      
 
        DBGridPreFocusRow   =   CurrentRow.cloneNode(true);//复制行对象        
        alert(DBGridPreFocusRow.cells.length);//弹出框显示cells长度为0
         
        for(var   i=0;i <=CurrentRow.cells.length   -   1;i++)
        {  
                  var   ccl   =   CurrentRow.cells(i).childNodes(0);
                  ccl.style.backgroundColor   =   RowColor;
                  ccl.style.color   =   TextColor;                  
        }              
}

运行后,弹出框显示cells长度为0,   即行对象没复制成功!请大家给看看,我这段代码写得有什么问题?

------解决方案--------------------
你把这个函数加在td上了还说table上了
------解决方案--------------------
这里复制成功了,但是cells长度肯定为0,因为你是clone出来的
只能通过DBGridPreFocusRow.childNodes来获得子节点,不能通过cells
保存全局变量DBGridPreFocusRow=CurrentRow;就可以了,为什么要clone呢

------解决方案--------------------
mark