javascript - 使用 nextSibling 从 HTML 表格中检索数据

标签 javascript

我有两个 HTML 表格:

<tr>
    <td width="319" align="right" bgcolor="#F5F5F5" class="txtdarkgreybold">Article title:</td>
    <td width="346" align="left" bgcolor="#F5F5F5">My title</td>
</tr>
<tr>
    <td align="right" class="txtdarkgreybold">Author:</td>
    <td align="left"><a href="/index.php?action=search&amp;name1=Przylipiak Mirosław&amp;results=10">Doe John</a><br /></td>
</tr>

通过使用 document.getElementsByClassName("txtdarkgreybold") 我可以检索字符串 Article title: 但我对它后面的内容感兴趣,即: 我的标题。我该怎么做呢?在第二个表中,我可以得到作者:,但我对Doe John感兴趣。我正在考虑将 nextSibling 作为获取我可以获得的字符串之后的字符串的一种方式,但我无法自己实现它。

最佳答案

您可以使用nextElementSibling textContent :

document.querySelector('.txtdarkgreybold').nextElementSibling.textContent

希望这有帮助。

console.log( document.querySelectorAll('.txtdarkgreybold')[0].nextElementSibling.textContent );

console.log( document.querySelectorAll('.txtdarkgreybold')[1].nextElementSibling.textContent );
<table>
  <tr>
    <td width="319" align="right" bgcolor="#F5F5F5" class="txtdarkgreybold">Article title:</td>
    <td width="346" align="left" bgcolor="#F5F5F5">My title</td>
  </tr>
  <tr>
    <td align="right" class="txtdarkgreybold">Author:</td>
    <td align="left"><a href="/index.php?action=search&amp;name1=Przylipiak Mirosław&amp;results=10">Doe John</a><br /></td>
  </tr>
</table>

关于javascript - 使用 nextSibling 从 HTML 表格中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40829932/

相关文章:

javascript - R Shiny – 使用 CSS 滚动时让 wellPanel 弹出窗口跟随

javascript - 使用未声明的变量进行解构 - 它们的范围是什么?

javascript - 通过链接将文本插入文本区域

javascript - React-Redux - 创建搜索过滤器

Javascript 函数在点击时添加/删除框边框

javascript - 如何合并对象?

javascript - 私有(private)文件夹中的重复文件

javascript - 创建 Canvas 矩阵

javascript - react 带 : how to make dropDown menu's size fill all of the available space

javascript - javascript中对象的笛卡尔积