我有以下代码:
$("table.altRow tr:visible").each(function (index) {
if (index % 2) {
$(this).addClass("oddColor");
} else {
$(this).addClass("evenColor");
}
});
但我现在需要在循环中单独为某些表运行此代码(您可以忽略我为什么要为这个问题执行此操作,因为我的问题更多是关于语法)。所以我想要这样的东西:
$("table.altRow").each(function () {
$(this + " tr:visible").each(function (index) {
if (index % 2) {
$(this).addClass("oddColor");
} else {
$(this).addClass("evenColor");
}
});
});
上面代码的问题是这一行:
$(this + " tr:visible")
我试图弄清楚如何编写代表循环中该表的选择,但在其后连接“tr:visible”。正确的语法是什么?
最佳答案
改变
$(this + " tr:visible")
至
$("tr:visible", this)
使用上下文,或者简单地
$(this).find("tr:visible")
关于javascript - 如何使用 jquery 连接 "this"和附加字符串选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20935561/