需求.我有一个动态生存的table 需要实现点击一行变色  点击第2行第一行还原 第2行变色 类推。
代码实现了点击变色 但是点击之后无法还原 希望JS高手帮忙改改。
PS: 不能直接在TR中加入客户端事件 按照下面这个 改 谢谢。function senfe(o, a, b, c)
{
    var t = document.getElementById(o)
    if (t != null)
    {
        t = t.getElementsByTagName("tr");
        for (var i = 1; i < t.length; i++)
        {
            var bool = true;
            t[i].onclick = function()
            {
                this.x = "0";
                this.style.backgroundColor = (this.sectionRowIndex % 2 == 0) ? a : b;
                this.color = "#ffffff";
                this.style.cursor = "hand";
            }
            t[i].onmouseover = function()
            {
                if (this.x != "1")
                    this.style.cursor = "hand";
            }
        }
    }
}

解决方案 »

  1.   

    asp.net .这段代码可以实现点击一行 但是点击之后 颜色无法还原 并且点击其他行 颜色还是在 这样效果就点击一行就产生一行颜色了
      

  2.   


    这里你想得太简单了。在设置之前,需要把原值保存起来,例如                this.old_x = this.x;
                    this.old_style_backgroundColor = this.style.backgroundColor;
                    this.old_color = this.color;
                    this.old_style_cursor = this.style.cursor;这样,当别的行被设置时,就要遍历其它行,将那些 this.color 与 this.old_color 不一致的行全都回复值。
      

  3.   

    t[i].onclick = function()             {               var trItems = this.Parent.getElementsByTagName("tr");  // 重新遍历复位这些tr ,这是最土的办法,但是能用,呵呵              this.x = "0";                 this.style.backgroundColor = (this.sectionRowIndex % 2 == 0) ? a : b;                 this.color = "#ffffff";                 this.style.cursor = "hand";             } 
      

  4.   


    var FocusTrNum = -1;    function senfe(o, a, b, c) {
            var t = document.getElementById(o).getElementsByTagName('tr');
            if (t != null) {
                for (i = 0; i < t.length; i++) {                var bool = true;                t[i].onclick = function (n) {
                        return function () {
                            if (FocusTrNum != -1 && FocusTrNum != n) {
                                t[FocusTrNum].style.backgroundColor = '';
                                FocusTrNum = n;
                            }                        this.style.backgroundColor = t[n].sectionRowIndex % 2 == 0 ? a : b;
                            this.style.color = "#fff";
                            this.style.cursor = "pointer";                        FocusTrNum = n;
                        };
                    }(i);                t[i].onmouseover = function (n) {
                        return function () {
                            if (this.x != "1") {
                                this.style.cursor = "pointer";
                            }
                        }
                    }(i)
                }
            }
        }