将变量传递给jquery选择器

时间:2014-08-29 19:34:46

标签: javascript jquery

这有效:

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(遥控器);

1 个答案:

答案 0 :(得分:6)

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

rowColors('remotes');