Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.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/78.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 在嵌套的div中遍历表行_Javascript_Jquery - Fatal编程技术网

Javascript 在嵌套的div中遍历表行

Javascript 在嵌套的div中遍历表行,javascript,jquery,Javascript,Jquery,我有一个包含两层嵌套div标记的页面。在div中的8或9是表。如何遍历div并选择所需的特定div,然后遍历表中嵌入在每个div中的一行的单元格?下面是我想反复浏览的一个具有代表性的页面示例 <div id="TheHouseDiv" class="catbox_m"> <div class="Room1Div"> <table width="900" border="0"> <tbody> <t

我有一个包含两层嵌套div标记的页面。在div中的8或9是表。如何遍历div并选择所需的特定div,然后遍历表中嵌入在每个div中的一行的单元格?下面是我想反复浏览的一个具有代表性的页面示例

<div id="TheHouseDiv" class="catbox_m">
  <div class="Room1Div">
     <table width="900" border="0">
       <tbody>
         <tr>
           <td><a href="/blah/blah1.html">I don't care about this value</a></td>
           <td><a href="/blah/blah2.html">I WANT THIS VALUE 1!</a></td>
           <td><a href="/blah/blah3.html">I WANT THIS VALUE TOO 2!</a></td>
           <td><a href="/blah/blah4.html">Another cell I don't want</a></td>
           <td><a href="/blah/blah5.html">THIS CELL I WANT ALSO</a></td>
         <tr>
       </tbody>
     </table>
     <table width="900" border="0">
       <tbody>
         <tr>
           <td><a href="/blah/blah1.html">Ignore this value in the second table</a></td>
           <td><a href="/blah/blah2.html">I WANT THIS VALUE</a></td>
           <td><a href="/blah/blah3.html">I WANT THIS VALUE TOO</a></td>
           <td><a href="/blah/blah4.html">Ignore this content</a></td>
           <td><a href="/blah/blah5.html">GET THIS CELL VALUE</a></td>
         <tr>
       </tbody>
     </table>
   </div>
   <div class="Room2Div">
     <table width="900" border="0">
       <tbody>
         <tr>
           <td><a href="/blah/blah1.html">I don't care about this value</a></td>
           <td><a href="/blah/blah2.html">I WANT THIS VALUE 1!</a></td>
           <td><a href="/blah/blah3.html">I WANT THIS VALUE TOO 2!</a></td>
           <td><a href="/blah/blah4.html">Another cell I don't want</a></td>
...
你明白了。因此,每个div和多个div中都有一个表。实际上有8到10个div。所有表或单元格都没有ID,因此我需要按位置引用。但是,我不想要所有的单元格或所有的表。我只需要每个div中每个表中特定单元格的值,尽管我需要每个表中相同的单元格。我会重复这个还是只引用我想要的特定单元格?如果是,我如何选择它们

$('#TheHouseDiv div').eq(1).find('table').eq(2).find('tr').eq(3).find('td').eq(4).text()
有点冗长,但我相信这会从HouseDiv中包含的第一个中检索第二个的第三个中的第四个文本

您也可以使用快捷方式。找到“tr:first”以匹配第一个快捷方式。

这将为您提供所需的所有tds。如果您不理解选择器,请发表评论,我会解释它

$("div#TheHouseDiv > div > table td:not(:nth-child(1)):not(:nth-child(4))")
e、 g.在这些tds内的标签的HREF上循环

e、 g.在这些tds内的标签文本上循环


我想这应该被命名为引用表格和单元格位置有任何反馈给我的答案吗?它不是你想要的吗?我不是很确定,但我认为>只在ie中有效,而且:n child在某些浏览器中也不起作用。@TeKapa你完全错了。jQuery用javascript处理选择器,不受浏览器功能的限制。这就是为什么我们使用像jQuery这样的跨浏览器库,它处理各个浏览器的所有怪癖,并保证此选择器在每个浏览器中的行为都是相同的。请删除此评论,这对一个没有经验的读者来说是非常误导的。这在一个完全不同的问题上帮助了我很多:
$("div#TheHouseDiv > div > table td:not(:nth-child(1)):not(:nth-child(4)) a")
    .each(function(i, ele) {
        alert(ele.href);
    }
);
$("div#TheHouseDiv > div > table td:not(:nth-child(1)):not(:nth-child(4)) a")
    .each(function(i, ele) {
        alert($(ele).text()); //or ele.innerHTML if no nasty is in the <a> tags
    }
);