javascript - 从 html 字符串中获取列值 - Javascript

标签 javascript html

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

相关文章:

javascript - jQuery + CSS : How to detect the first "move out" of cursor out of an element?

javascript - 从 CDN 加载 Ace 编辑器

javascript - KendoUI 网格不会触发 saveChanges 事件

javascript - ondblclick 不适用于带有 onclick javascript 的元素

javascript - id 的 jquery/javascript 选择器以特定文本开始并获得它结束的最高值

javascript - 搜索字符串是否存在于数组的任何值中,例如indexOf

javascript - 什么 (函数(){})();意思是?

html - 悬停时更改颜色无法正常工作

html - 增加嵌套 (ul ul ul li) 下拉导航列表项的宽度

javascript - 使用 angularjs 过滤器显示包含特定 css 类的元素