javascript - jQuery 如何捕获 tbody 中的嵌套值?

标签 javascript jquery dom

我想获取tbody中嵌套值的值。这是一个说明我的意思的示例。

在示例中,我想获取存储在 href 中的所有值。

<table id="toc" class="plainlinks" style="text-align: center" align="center">
  <tbody>
    <tr>
      <td><b>Index</b></td>
    </tr>
    <tr>
      <td>
        <p>
          <a class="external text" href="//nl.wiktionary.org/w/index.php?title=Categorie:Zelfstandig-naamwoordsvorm_in_het_Nederlands&amp;from=A">A</a>
          - <a class="external text" href="//nl.wiktionary.org/w/index.php?title=Categorie:Zelfstandig-naamwoordsvorm_in_het_Nederlands&amp;from=Ab">Ab</a>
          - <a class="external text" href="//nl.wiktionary.org/w/index.php?title=Categorie:Zelfstandig-naamwoordsvorm_in_het_Nederlands&amp;from=Ad">Ad</a>
          - <a class="external text" href="//nl.wiktionary.org/w/index.php?title=Categorie:Zelfstandig-naamwoordsvorm_in_het_Nederlands&amp;from=Ag">Ag</a>
          - <a class="external text" href="//nl.wiktionary.org/w/index.php?title=Categorie:Zelfstandig-naamwoordsvorm_in_het_Nederlands&amp;from=Al">Al</a>
          - <a class="external text" href="//nl.wiktionary.org/w/index.php?title=Categorie:Zelfstandig-naamwoordsvorm_in_het_Nederlands&amp;from=Ap">Ap</a>
          - <a class="external text" href="//nl.wiktionary.org/w/index.php?title=Categorie:Zelfstandig--naamwoordsvorm_in_het_Nederlands&amp;from=Ap1">Ap1</a>
        </p>
      </td>
    </tr>
  </tbody>
 </table>

我尝试了以下语句来捕获值:

$('table tr').each(function (index, value) {
  $('td' ,this).each(function (index, value) {
    $('p',  this).each(function (index, value) {
      $('a',this).each(function (index, value) {
        console.info($(this).html())
      });
    });
  });
});

最佳答案

$('table tbody a').each(function(i,el){
console.info($(el).attr('href'));
})

将取“tbody”中的所有“a”

关于javascript - jQuery 如何捕获 tbody 中的嵌套值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41296367/

相关文章:

javascript - 在印象中执行第三方 JavaScript 片段

javascript - 限制在 Node.js 中的循环中产生的并发子进程数

javascript - 在页面最初加载时执行淡入?

javascript - 如何在 DOM 上触发 js 创建的触摸事件

javascript - document.open 如何影响历史?

javascript - 如果 val() 不是未定义的,则查找具有特定数据属性的所有元素

javascript - 在 JavaScript 对象中定义函数的简写

javascript - 哪个 jQuery 事件处理程序适用于页面加载?

jquery - 单击触发它显示的链接时,如何触发 Spotify 播放按钮播放?

javascript - 什么时候 event.target.value 不是字符串?