我从服务器收到类似于以下内容的 html 字符串的响应。请注意,总是有一个数据行:
<html>
<head>
<title>Geoserver GetFeatureInfo output</title>
</head>
<body>
<table class="featureInfo">
<caption class="featureInfo">CATAST_Pol_ParcelaRusti</caption>
<tr>
<th>fid</th>
<th >FEATURE</th>
<th >REFCAT</th>
<th >CMUNICIPIO</th>
<th >MUNICIPIO</th>
</tr>
<tr>
<td>CATAST_Pol_ParcelaRusti.109</td>
<td>200007</td>
<td>1010232</td>
<td>1</td>
<td>ABÁIGAR</td>
</tr>
</table>
<br/>
</body>
</html>
Is there a way for programmatically get the
REFCAT
value (=1010232
) from the html string?
提前致谢。
最佳答案
您可以使用 cellIndex
let th = Array.from(document.querySelectorAll('.featureInfo th'))
.find(el => el.innerText === 'REFCAT')
let td = Array.from(document.querySelectorAll('.featureInfo td'))
.find(el => el.cellIndex === th.cellIndex)
console.log(td.innerText)
<table class="featureInfo">
<caption class="featureInfo">CATAST_Pol_ParcelaRusti</caption>
<tr>
<th>fid</th>
<th>FEATURE</th>
<th>REFCAT</th>
<th>CMUNICIPIO</th>
<th>MUNICIPIO</th>
</tr>
<tr>
<td>CATAST_Pol_ParcelaRusti.109</td>
<td>200007</td>
<td>1010232</td>
<td>1</td>
<td>ABÁIGAR</td>
</tr>
</table>
关于javascript - 从 html 字符串中获取列值 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57766964/