将变量传递给jquery选择器

Passing a variable to a jquery selector

本文关键字:jquery 选择器 变量      更新时间:2023-09-26

这很有效:

function rowColors(){
     $("table#remotes th").css("background-color", "#202020");
     $("table#remotes tr").css("background-color", "#484848");
     $("table#remotes tr:visible:odd").css("background-color", "#333333");
 }

如何使用可变表id实现此功能?这不起作用:

function rowColors(tblid){
    $("table#"+tblid+" th").css("background-color", "#202020");
    $("table#"+tblid+" tr").css("background-color", "#484848");
    $("table#"+tblid+" tr:visible:odd").css("background-color", "#333333");
}

rowColors(遥控器);

可能是因为remotes是一个变量而不是字符串

rowColors('remotes');