2015-07-10 152 views
1

以下是我的表,我想从表中选择元素jQuery选择不选择值

<table id="DetailsGridViewDiv_Header" style="border: 1px solid;"> 
    <tbody> 
     <tr> 
      <th>A</th> 
      <th>B</th> 
      <th>C</th> 
      <th>D</th> 
      <th>E</th> 
      <th>F</th> 
      <th>G</th> 
      <th>H</th> 
     </tr> 
    </tbody> 
</table> 

这是我想从表中提取值:

alert($("#DetailsGridViewDiv_Header > th:nth-child(3)").text()); 

它不返回任何字母

+1

大声笑,@Rory,那是什么在第一编辑:d – nicael

+1

@尼卡尔该死的,我以为我会逃避它:DI有我在剪贴板中编辑过的另一个问题,并不小心将它粘贴在 –

回答

1

th不与ID DetailsGridViewDiv_Header表的直系后裔,所以应该是:

$("#DetailsGridViewDiv_Header th:nth-child(3)").text() 
2

的问题是,因为你使用的嫡系选择(>)和th不是#DetailsGridViewDiv_Header的直系后裔。删除操作,它会很好地工作:

alert($("#DetailsGridViewDiv_Header th:nth-child(3)").text()); 

如果你想保持后代选择,您将需要遵循后代元素依次是:

alert($("#DetailsGridViewDiv_Header > tbody > tr > th:nth-child(3)").text()); 
1

当你在jQuery的它选择的东西返回一个数组,所以它可能更有意义,使用EQ()函数来选择jQuery对象(感谢,罗里!):

var $thArray = $("#DetailsGridViewDiv_Header").find("th"); 
alert($thArray.eq(2).text()); 
+1

请注意,这会给你一个错误 - 你在DOMElement上调用text(),而不是在jQuery对象上调用。 –

+0

不,find()返回一个jQuery对象。 – SeanKendle

+2

没错,但通过索引('$ thArray [2]')访问它会返回一个DOMElement。 –