如何仅为没有奇数/偶数类的表行添加奇数/偶数类? [英] How do I add odd/even class only for table rows which haven´t had odd/even class yet?

查看:278
本文介绍了如何仅为没有奇数/偶数类的表行添加奇数/偶数类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅为没有奇数/偶数类的表行添加奇数/偶数类?
(对于某些表我使用PHP循环函数)。

How do I add odd/even class only for table rows which haven´t had odd/even class yet? (for some tables I use PHP cycle function).

这是正确的吗?

$("table tbody tr td")
  .parent("tr:nth-child(odd):not(.odd)")
  .addClass("odd")
  .end()
  .parent("tr:nth-child(even):not(.even)")
  .addClass("even");


推荐答案

如果已经有jQuery,它将不会复制该类定义。您当然可以使用 .hasClass()进行检查,但在这种情况下没有必要。

jQuery will not duplicate the class if it has already been defined. You can of course always check using .hasClass() but it is not necessary in this scenario.

尝试:

$("table tbody tr:nth-child(odd)").addClass("odd");
$("table tbody tr:nth-child(even)").addClass("even");

:从您的问题看来一些,但不是所有行都已经添加了奇数或偶数类。最安全的方法是删除所有类并使用JS添加它们 - $(table tbody tr)。removeClass(odd even)

: From your question is seems that some, but not all, rows have the "odd" or "even" classes already added to them. The safest way is to strip all classes and add them back using JS - $("table tbody tr").removeClass("odd even")

这篇关于如何仅为没有奇数/偶数类的表行添加奇数/偶数类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆