单击按钮时突出显示行

当我单击该行的编辑按钮时,我正在使用以下脚本突出显示行。 当我点击按钮时,我正在传递Id的行! 我的问题是代码在Mozila Firefox中运行但在Google Chrome上运行。 以下代码有什么问题。

function high(id) { $('tr').removeAttr('style'); document.getElementById(id).style="background-color:#eeeeea;color:#000000;font-weight:500;"; } 

试试这个,

 $('#'+id).attr("style","background-color:#eeeeea;color:#000000;font-weight:500;"); 

也在使用chrome。

原因可能是样式是一个在其中具有某些属性的对象,并且chrome可能不允许覆盖它。 因此,您在样式中传递的自定义字符串将不适用于它,因此不会对该元素应用任何更改。

这是一个演示,用于向编辑行添加特殊类,并删除其他行上的类。 这是使用jquery的nearest()方法完成的。 你甚至不需要为此使用任何id。

 $("button").on("click",function(){ $("tr").each(function(){ $(this).removeClass("marked"); }); $(this).closest("tr").addClass("marked"); }); 
  .marked{ background-color:#eeeeea;color:#000000;font-weight:500; } 
  
This is TD
This is TD
This is TD

您需要单独设置样式对象的属性。

 var elem = document.getElementById(id); //Set properties elem.style.color = "#000000"; elem.style.fontWeight = "500"; elem.style.backgroundColor = "#eeeeea"; 

由于您使用的是jquery ,因此可以使用.css()

 $('#' + id).css({ "background-color" :"#eeeeea", "color":"#000000", "font-weight":"500"; }); 

但是,我建议你创建一个CSS类然后使用它。

 $('tr').removeClass('highlight'); $('#' + id).addClass('highlight');