我从服务器收到类似于以下内容的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>



  有没有办法以编程方式获取REFCAT值(=1010232
  从HTML字符串?


提前致谢。

最佳答案

您可以使用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>

10-07 14:11