2015-07-05 239 views
0

此问题可能会被复制,但我没有找到有用的东西。
不管怎么说,这是我的HTML代码:获取jQuery元素的第二个子元素

<table> 
<thead> 
<tr> 
    <th>foo</th> 
    <th>foo</th> 
    <th>foo</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
    <td>bar</td> 
    <td>bar</td> 
    <td>bar</td> 
</tr> 
</tbody> 

这是我的jQuery代码:

$("table tbody tr").hover(

    function() { 
     var secondCell = $(this).children[1].textContent; 

     //secondCell.someCode 
    }, 

    function() { 
     //some code 
    } 

); 

所有我想要做的是:
当玩家徘徊一行时,它应提醒他们是一条消息,并且该消息具有第二个单元格文本。
希望你明白了,并提前致谢。

+0

$(“tr td:nth-​​child(2)”)。 – Kiloreux

回答

0

在jquery中,.children()函数。所以你需要先调用它,然后才能从数组中获取元素。看看jquery .children()文档。

您可以像这样使用它:jsfiddle

+0

哦,是的,我看到了这个,但是当我输入它时,我的IDE说'children'不是函数,它是一个数组,但是当我尝试它时它工作。 – PepsiGam3r

1

有几种方法:

$("tr td:nth-child(2)")

$("tr").children().eq(1)

$("tr td").eq(1)

$("tr td").filter(":nth-child(2)")

+0

谢谢habibi,但是你知道,我想要使用$(this)获得已经选中的行的单元格,谢谢。 – PepsiGam3r

0

您可以使用下面的代码也

$("table tbody tr").hover(

    function() { 
     var secondCell = $(this).find("td:eq(1)").text(); 

     //secondCell.someCode 
    }, 

    function() { 
     //some code 
    } 

); 
相关问题