leo*_*ora 0 javascript jquery concatenation selector
我有以下代码:
$("table.altRow tr:visible").each(function (index) {
if (index % 2) {
$(this).addClass("oddColor");
} else {
$(this).addClass("evenColor");
}
});
Run Code Online (Sandbox Code Playgroud)
但我现在需要在一个循环中单独为一些表运行此代码(你可以忽略我为什么这样做这个问题,因为我的问题更多是关于语法).所以我想要这样的东西:
$("table.altRow").each(function () {
$(this + " tr:visible").each(function (index) {
if (index % 2) {
$(this).addClass("oddColor");
} else {
$(this).addClass("evenColor");
}
});
});
Run Code Online (Sandbox Code Playgroud)
上面代码的问题是这一行:
$(this + " tr:visible")
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚如何在循环中编写表示该表的选择,但在它之后连接"tr:visible".这个的正确语法是什么?
更改
$(this + " tr:visible")
Run Code Online (Sandbox Code Playgroud)
至
$("tr:visible", this)
Run Code Online (Sandbox Code Playgroud)
使用上下文,或简单地说
$(this).find("tr:visible")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |