javascript - 如何在页面加载时突出显示 JQuery 表的第一行?

标签 javascript jquery html syntax-highlighting highlight

如果单击任何行,我的以下代码可以正常工作:

$(document).ready(function() {  
$('#currentloc_table tr').click(function(event) {
    $("#"+$(this).attr('id')+" input:checkbox")[0].checked = !  ($("#"+$(this).attr('id')+" input:checkbox")[0].checked);
    if($("#"+$(this).attr('id')+" input:checkbox")[0].checked == true)
        $(this).addClass('selected');
    else if($("#"+$(this).attr('id')+" input:checkbox")[0].checked == false)
        $(this).removeClass('selected');    
});
});

这是 CSS:

.selected td {
background: yellow;
}

我需要在页面加载时,该表的第一行应该突出显示。我怎样才能做到这一点?

最佳答案

试试这个

$(document).ready(function () {
    $('#currentloc_table tr').click(function (event) {
            $("#" + $(this).attr('id') + " input:checkbox")[0].checked = !($("#" + $(this).attr('id') + " input:checkbox")[0].checked);
            if ($("#" + $(this).attr('id') + " input:checkbox")[0].checked == true)
               $(this).addClass('selected');
            else if ($("#" + $(this).attr('id') + " input:checkbox")[0].checked == false)
                $(this).removeClass('selected');
    });
    // Add these lines
    var firstTr = $('#currentloc_table tr:first');
    firstTr.addClass('selected');
    firstTr.find("input:checkbox")[0].checked = true;
});

正在工作 demo

关于javascript - 如何在页面加载时突出显示 JQuery 表的第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14909438/

相关文章:

javascript - 更改 css 属性取决于是否使用右键按下的条件

python - 用 Python 编写的 HTML 缩进器

html - 选择背景为 :inherit bug 的选项

javascript - dojo 或 javascript 中的日期字符串格式

javascript - 如何在 Relay Modern 中使用静态数据设置本地存储

javascript - 尝试使用 toastr 时出现错误 "toastr is not a function error"

jquery - 带有传入变量的 SlideUp 回调?

Javascript 将输入文件传递给 PHP

html - 在 html 问题中创建括号?

javascript - 如何使用 javascript 获取完整日期?