2013-01-25 84 views
0

我已经能够选择我想要的特定行,但是我无法从那些tr中选择特定的td。在选定的tr中选择特定的tr和特定的td

我用这个来选择行:

$('#time-slot tr:not(:first, :last)') 

那么这些选定的行,我试图忽略第一个和最后TD内,但它无法正常工作。我用类似的方法,如上

$('#time-slot tr:not(:first, :last) td:not(:first, :last)') 

任何想法我应该如何解决这个问题。顺便说一句我想单击并拖动鼠标以用户定义的颜色绘制单元格。

+0

ü可以告诉我UR表结构? – muthu

+0

尝试使用'not'和'filter'方法。 – elclanrs

+0

不明白你的问题。你的代码似乎工作我测试它。什么是你的问题? – Jai

回答

1

我更喜欢使用较少的字符串选择和更多的jQuery方法,以及使用:first-child:last-child选择您的问题:

$("#time-slot").find("tr").not(":first-child, :last-child").find("td").not(":first-child, :last-child").addClass("active"); 

http://jsfiddle.net/7b4Ms/

根据您所期待,你没有解释得很清楚,这可能是也可能不是你想要的。它所做的是选择不是第一个和最后一个的所有tr元素。在匹配的tr元素内部,它会选择所有不是第一个和最后一个的td元素。所以基本上,它不会选择table中的单元格的外部环。

+0

OP的代码工作正常。为什么这种方法? – Jai

+0

@Jai它工作正常吗?那他们为什么要问一个问题?他们说他们不能选择他们想要的特定“td”元素。这是我对他们期望的解释。它可能**工作,但它似乎并不正确地在他们的意见中工作,因此他们的问题。 – Ian

+0

多数民众赞成但罚款,但代码工作,应该问这个问题? – Jai

0

完全同意伊恩,只是我更喜欢使用.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"); 
+1

你在说什么样的问题? – SherCoder

+0

我绝对同意,但也许需要选择嵌套的东西。原来使用的选择符是'#time-slot tr:not(:first,:last)'(意思是查找所有的后代......与.find()')相同,而不是'#time-slot> tr:not :first,:last)'(意思是找到所有的孩子......和'.children()'相同) – Ian

0

试试这个(只使用CSS选择器):

$("#time-slot tr:not(:first,:last) td:not(:first-child,:last-child)").addClass("active"); 

Sample