javascript - 使用 JavaScript 从特定表中获取特定行?

标签 javascript html css-tables

假设我的动态 HTML 看起来像这样:

<table id="DanishCompanies">
  <tr><th>Name</th><th>Employees</th><th>Founded</th></tr>
  <tr id="19"><td>Company A</td><td>80</td><td>1980</td></tr>
  <tr id="17"><td>Company B</td><td>12</td><td>1910</td></tr>
  <tr id="26"><td>Company C</td><td>5000</td><td>2015</td></tr>
</table>
<table id="SwedishCompanies">
  <tr><th>Name</th><th>Employees</th><th>Founded</th></tr>
  <tr id="10"><td>Company D</td><td>500</td><td>1950</td></tr>
  <tr id="12"><td>Company E</td><td>900</td><td>1990</td></tr>
  <tr id="17"><td>Company F</td><td>90</td><td>2010</td></tr>
</table>
<table id="NorwegianCompanies">
  <tr><th>Name</th><th>Employees</th><th>Founded</th></tr>
  <tr id="17"><td>Company G</td><td>105</td><td>1970</td></tr>
  <tr id="18"><td>Company H</td><td>100</td><td>1980</td></tr>
  <tr id="19"><td>Company I</td><td>45</td><td>2000</td></tr>
</table>

每个 tr 都有一个 ID,但 ID 只是表的相对唯一,因为其他表可能有 ID,并且行数可能会有所不同。

如何获取 ID 为 17 的瑞典公司的成立年份(第 2 列)?

我想你会这样做,但我找不到正确的代码。

var table = document.getElementById("SwedishCompanies");
var row_index = ??? //should return 2
return table[row_index].cells[2].innerHTML;

我不能仅使用 getElementById 来获取 ID“17”,因为我会冒着获取丹麦语或挪威语公司的风险,因为这些表的顺序是随机的。

最佳答案

你只是没有使用正确的选择器,

#DanishCompanies tr[id="17"]

将为您提供 ID 为 17tr,它是 DanishCompanies 的 child :

const row = document.querySelector('#DanishCompanies tr[id="17"]');
const year = row.cells[2].innerHTML;
console.log(year);
<table id="DanishCompanies">
  <tr>
    <th>Name</th>
    <th>Employees</th>
    <th>Founded</th>
  </tr>
  <tr id="19">
    <td>Company A</td>
    <td>80</td>
    <td>1980</td>
  </tr>
  <tr id="17">
    <td>Company B</td>
    <td>12</td>
    <td>1910</td>
  </tr>
  <tr id="26">
    <td>Company C</td>
    <td>5000</td>
    <td>2015</td>
  </tr>
</table>
<table id="SwedishCompanies">
  <tr>
    <th>Name</th>
    <th>Employees</th>
    <th>Founded</th>
  </tr>
  <tr id="10">
    <td>Company D</td>
    <td>500</td>
    <td>1950</td>
  </tr>
  <tr id="12">
    <td>Company E</td>
    <td>900</td>
    <td>1990</td>
  </tr>
  <tr id="17">
    <td>Company F</td>
    <td>90</td>
    <td>2010</td>
  </tr>
</table>
<table id="NorwegianCompanies">
  <tr>
    <th>Name</th>
    <th>Employees</th>
    <th>Founded</th>
  </tr>
  <tr id="17">
    <td>Company G</td>
    <td>105</td>
    <td>1970</td>
  </tr>
  <tr id="18">
    <td>Company H</td>
    <td>100</td>
    <td>1980</td>
  </tr>
  <tr id="19">
    <td>Company I</td>
    <td>45</td>
    <td>2000</td>
  </tr>
</table>

关于javascript - 使用 JavaScript 从特定表中获取特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67040785/

相关文章:

html - 使用 CSS 的等高列

css - 宽度为 : 100% inside position: absolute on IE7 的表格

jquery - 使 div 保持包含在其父鼠标移动中

html - 在 HTML 中寻找显示包含特定属性的所有元素的引用

javascript - 如何在 Javascript 鼠标悬停时将 css 类设置为 <a> 标签数组?

javascript - 使用 Jasmine 和 Karma 测试 Ionic - 未捕获的 ReferenceError : angular is not defined

javascript - 重制这个正则表达式

html - 无填充对齐元素

javascript - 通过按钮更改 <p> 之间的文本

javascript - 使用正则表达式作为模板