使用jquery更新表行的背景色

Update background color of table row using jquery

本文关键字:背景色 jquery 更新 使用      更新时间:2023-09-26

我使用以下jquery将外部html页面加载到名为content的div中:

$(document).ready(function(){
     $("#content").load("content.html");
});
(function($) {  
 $(function() {  
 $('.load_link').click(function() {  
    $("#content").load("content2.html");  
    return false;  
 });  
});  
})(jQuery); 

触发内容更改的链接(使用<a class="load_link">)位于表中的一行中。当有人单击链接时,我希望能够更新分配给表中一行的类。包含单击的链接的行应分配"rowselected"类,所有其他行(仅在该表中)应分配"rownotselected)"类。只有该表中的行使用这些类名,因此替换该类名的任何出现都应该是安全的。

这可能吗?我是第一次使用jquery。

提前感谢您的建议!

怎么样:

 $('.load_link').click(function() {  
    $("#content").load("content2.html");  
    var $row = $(this).closest("tr");
    // find the parent <tr> and set the class
    $row.removeClass("rownotselected").addClass("rowselected");
    // Set the class of the other rows:
    $row.siblings("tr").removeClass("rowselected").addClass("rownotselected");
    return false;  
 }); 
  • 使用closest()查找父tr
  • 使用removeClassaddClass删除相应的类
  • 使用siblings()查找当前表中的同级行

下面是一个工作示例:http://jsfiddle.net/andrewwhitaker/vN2ny/