jQuery选择器用于查找td的内容

jQuery选择器用于查找td的内容,jquery,Jquery,我的HTML结构如下所示: <div id="mainDiv"> <table></table> <table></table> <table> <tr> <td id="first">FirstCell</td><td id="second">SecondCell</td> </tr> <tr> <td></td>&

我的HTML结构如下所示:

<div id="mainDiv">
<table></table>
<table></table>
<table>
<tr>
<td id="first">FirstCell</td><td id="second">SecondCell</td>
</tr>
<tr>
<td></td><td></td>
</tr>
</table>
</div>
我们将非常感谢您的帮助

.value()
不是函数。您可能正在查找
.text()


看看你的结构,我发现你在寻找相对于第一个的第二个。是否有多个元素具有
id=“second”
?如果是这样的话,那么jQuery就会遇到很多麻烦,因为
id
属性对于单个元素来说是唯一的。如果属性用于标识多个元素,则应该使用
class
而不是
id

基于html结构,您可以像这样实现您想要的

$("#mainDiv tr td:contains('First')").each(function(){
                var secondCell = $(this).next().text();
                console.log(secondCell);
            });​


就像@Blender所说的
.value()
不是函数,请改用。

value不是有效的方法

您应该根据需要使用其中一种

.html()  // for html content.
.text()  // for text content.
.val()   // for value attribute. Like value of input element.

在使用每个时,应使用类名而不是id,请尝试以下操作:

$("#mainDiv tr td:contains('first')").each(function(){
    var secondCell = $(this).siblings('.second').text();
});

当你说find时,你是指使用regex吗?或者通过与第一个的关系

如果通过关系,那么这将起作用

$("#mainDiv tr td:contains('first')").each(function(){
            var secondCell = $(this).next().html();
        });
.text()有时优于.html(),具体取决于您所做的操作。有关工作示例和更多信息,请参阅


如果使用正则表达式,则必须在第一个

change
var secondCell=$(this.find(“#second”).value()中提供所要查找的内容
to
var secondCell=$(this).next().text()我不会列出
.val()
。这个函数只用于输入元素,只会让人感到困惑。我在评论中提到过这一点。
$("#mainDiv tr td:contains('first')").each(function(){
            var secondCell = $(this).next().html();
        });