2011-08-29 17 views
0

我正在生成一个动态表,该动态表可以根据用户输入具有多个行数。当用户选择/点击任何行时,我想更改该行的背景颜色。我的代码如下所示:(注释的部分是我发审,但他们不工作)更改动态表中给定行(onClick事件)的背景颜色

function Contact_OnUpdateTelephone() { 
    $('#tableTelephone > tbody > tr').remove(); 
    for (var c = 0; c < _updatedTelephoneList.length; c++) { 
     var index = _updatedTelephoneList[c].Index; 
     var id = _updatedTelephoneList[c].Id; 

     $('#tableTelephone tbody:last').append("<tr onclick = GetTelephoneData(" + index + ",'" + id + "');><td>" + index + "</td><td>" + id + "</td></tr>");   
    }  
} 



function GetTelephoneData (index, id) { 

//Change the color of the clicked (selected) row 

//  $("#tableTelephone tbody tr").removeClass("altcol_blue"); 
//  $(this).addClass("altcol_blue"); 

//  $("tr").click(function(){ 
//   $(this).addClass("altcol_blue").siblings("tr").removeClass("altcol_blue"); 
//  }); 

     $("#tableTelephone tr td").live('click', function() { 
      $(this).addClass("altcol_blue").siblings("tr").removeClass("altcol_blue"); 
     }); 

Var SelectedIndex = index; 
... 
... 

    } 

我的CSS是这样的:

table.resulttable tr.altcol_blue td{ height:20px; font-family:Arial, Helvetica, sans-serif; font-weight:normal; color:#444444; font-size:11px; background-color:#deedf5; vertical-align:middle; padding-left:5px;} 
+0

'.live'处理程序在一个似乎是可重复函数的内部看起来相当错误。它应该设置一次。 – Zlatev

+0

非常真实...由于前两种方法没有奏效,所以我试图通过引入另一个Live事件,这不是必需的......它也没有工作。 – Biki

回答

0

根据您的文章,我想你想知道你怎么能改变背景颜色。

如果你想从我highligting只有一排下面一个想法:

$("#tableTelephone tr td").each(function() { 
     if ($(this).parent().hasClass("altcol_blue") == false) 
     { 
      $("#tableTelephone tr").removeClass("altcol_blue"); 
      $(this).parent().addClass("altcol_blue"); 
     } 
    }); 

这种方式背后的想法是,以确保该类的altcol_blue“将从旧行被删除(S )。