[英]select specific tr and specific td within selected tr
我已经能够选择所需的特定行,但无法从这些tr中选择特定的td。
我用它来选择行:
$('#time-slot tr:not(:first, :last)')
然后在那些选定的行中,我试图忽略第一个和最后一个td,但是它不起作用。 我用上面类似的方法
$('#time-slot tr:not(:first, :last) td:not(:first, :last)')
任何想法我应该如何解决这个问题。 顺便说一句,我试图单击并拖动鼠标以使用用户定义的颜色绘制单元格。
我更喜欢使用更少的字符串选择器和更多的jQuery方法,以及使用:first-child
和:last-child
选择器解决问题:
$("#time-slot").find("tr").not(":first-child, :last-child").find("td").not(":first-child, :last-child").addClass("active");
根据您的期望(您没有很好地解释),这可能不是您想要的。 这是选择所有不是第一个和最后一个的tr
元素。 在那些匹配的tr
元素内,它选择不是first和last的所有td
元素。 因此,基本上,它不会选择table
中单元格的外环。
完全同意Ian,只是我更喜欢使用.children()而不是.find()。 因为如果您有嵌套表,可能会造成问题。
$("#time-slot").find("tr").not(":first-child, :last-child").find("td").not(":first-child, :last-child").addClass("active");
更改为:
$("#time-slot").children("tr").not(":first-child, :last-child").children("td").not(":first-child, :last-child").addClass("active");
试试看(仅使用css选择器):
$("#time-slot tr:not(:first,:last) td:not(:first-child,:last-child)").addClass("active");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.