javascript - 如何使用 jquery 连接 "this"和附加字符串选择器?

标签 javascript jquery concatenation selector

我有以下代码:

            $("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/

相关文章:

javascript - jQuery:选择父列表项

javascript - 调整窗口大小时将元素移动到屏幕中央

javascript - 使用 Javascript 添加自定义 http header 并触发文件下载

javascript - [CORS] :' Access-Control-Allow-Origin' 不允许获取跨域 JSON?

javascript - FullPage.JS 滚动

jquery - Coldfusion jQuery getJSON : Getting WDDX instead of JSON

sql - 我应该如何修改这个 SQL 语句?

javascript - 如何在 Jquery Mobile 1.4.5 中刷新新的 Div 内容

concatenation - Perl 6 没有滑动的列表连接?

javascript - 在 Javascript 中调用串联变量