Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery遍历html表行以获取内部的div id<;td>;细胞_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 使用jquery遍历html表行以获取内部的div id<;td>;细胞

Javascript 使用jquery遍历html表行以获取内部的div id<;td>;细胞,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我希望遍历表id=“myTable”中每行中的每个单元格,并获取这些单元格的子div的id 到目前为止,我有一些类似于: $("td.theCell").each(function() { var id = $(this).attr('id'); //this would give me the td id but I want its child div's id }); 我的html类似于: <table id='myTable'> <tr>foo</

我希望遍历表id=“myTable”中每行中的每个单元格,并获取这些单元格的子div的id

到目前为止,我有一些类似于:

$("td.theCell").each(function() {
  var id = $(this).attr('id'); //this would give me the td id but I want its child div's id
});
我的html类似于:

<table id='myTable'>
  <tr>foo</tr>
  <tr>bar</tr>
    <td>foo</td>
    <td class='theCell'>
      <div id='myDiv'>foo</div>
    </td>
  <tr>foo</tr>
  <tr>bar</tr>
</table>

福
酒吧
福
福
福
酒吧
有人知道这样做的好方法吗?提前感谢。

用于获取后代div:

$("td.theCell").each(function() {
  var id = $(this).find('div').attr('id'); 
});
试试这个

$("td.theCell").each(function() {
  var id = $(this).find('div').attr('id'); 
});
您需要获得div的Id,而不是td.thecell元素。
因此,您需要使用
.find()
来选择其中的潜水..

将所有潜水ID保存在一个数组中。使用
。每个


您也可以直接选择子div,而不使用find:

$("td.theCell div").each(function() {    
      var id = this.id
});​

您只需在选择器中做一点小小的更改即可完成此操作

$("#myTable tr > td.theCell > div").each(function() {//you can remove the '>' if you don't just want the direct children only 
  var k = $(this).attr('id'); 
  console.log(k);
});​

$(“td.theCell div”).each(function(){var id=$(this.attr('id');//这会给我td id,但我想要它的子div的id})如果不是我有呢?使用$('td.theCell foo').each(function(){var id=$(this.find('div').attr('id');})有效吗;那么您的选择器应该是$(“td.theCell.foo”)。。其余的将是相同的
$("#myTable tr > td.theCell > div").each(function() {//you can remove the '>' if you don't just want the direct children only 
  var k = $(this).attr('id'); 
  console.log(k);
});​