使用纯JavaScript获取HTML表中特定单元格的值 [英] Get value of a specific cell in HTML table with pure JavaScript

查看:103
本文介绍了使用纯JavaScript获取HTML表中特定单元格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有React应用程序,该应用程序使用HTTP请求获取一些数据.响应内容包含一个格式化的HTML表,如下所示:

I have React application that fetches some data with HTTP request. Response content contains a formatted HTML table and looks like this:

<html>
  <head>
    <title>Geoserver GetFeatureInfo output</title>
  </head>
  <style type="text/css">
    table.featureInfo, table.featureInfo td, table.featureInfo th {
        border:1px solid #ddd;
        border-collapse:collapse;
        margin:0;
        padding:0;
        font-size: 90%;
        padding:.2em .1em;
    }
    table.featureInfo th {
        padding:.2em .2em;
        font-weight:bold;
        background:#eee;
    }
    table.featureInfo td{
        background:#fff;
    }
    table.featureInfo tr.odd td{
        background:#eee;
    }
    table.featureInfo caption{
        text-align:left;
        font-size:100%;
        font-weight:bold;
        padding:.2em .2em;
    }
  </style>
  <body>

<table class="featureInfo">
  <caption class="featureInfo">v_zgrada_gs</caption>
  <tr>
  <th>fid</th>
    <th >id</th>
    <th >parcela_id</th>
    <th >kc_broj</th>
    <th >ko_id</th>
    <th >ko</th>
    <th >zk_ulozak</th>
    <th >vrsta_id</th>
    <th >vrsta_zgrade</th>
    <th >izvor</th>
    <th >etaziranost</th>
    <th >uskladjeno</th>
    <th >kbr</th>
    <th >opis</th>
    <th >broj_katova</th>
    <th >povrsina_sluzbena</th>
    <th >povrsina</th>
    <th >godina_izgradnje</th>
    <th >izvor_podataka_id</th>
    <th >nedovrseno</th>
  </tr>

    <tr>

  <td>v_zgrada_gs.fid-752e8cc0_16cf6d070ba_-7146</td>    
      <td>1165</td>
      <td>10642</td>
      <td>1002/7</td>
      <td>3</td>
      <td>Novalja</td>
      <td>417</td>
      <td>2</td>
      <td>kuća</td>
      <td>DKP</td>
      <td>false</td>
      <td>false</td>
      <td></td>
      <td></td>
      <td>2.0</td>
      <td></td>
      <td>110.8</td>
      <td></td>
      <td>2</td>
      <td>false</td>
  </tr>
</table>
<br/>

  </body>
</html>

我有一个组件,该组件使用一种从响应中提取一个值的方法.我需要的值在表的第二个< td> 元素中(< td> 1165</td> ).

I have a component that uses a method to extract one value from response. The value I need is in second <td> element in table (<td>1165</td>).

方法如下:

getId = (response) => {
        /* 
            Missing code here
        */
        return id;
    }

我需要将第二个< td> 元素的值存储到名为'id'的变量的纯JavaScript代码."response"变量包含上面显示的原始HTML,我需要的id始终是第二个< td> 元素.

I need pure JavaScript code that stores value of second <td> element to variable named 'id'. 'response' variable contains raw HTML shown above, and the id I need is always second <td> element.

编辑:我需要解析在响应参数中传递的文本.文本包含HTML代码,但未在页面上的任何位置呈现.

I need to parse text that is passed in response argument. Text contains HTML code, but it is not rendered anywhere on page.

推荐答案

您可以使用正则表达式从HTML响应中提取第二个值.

You can use a regular expression to extract the 2nd value from your HTML response.

var pattern = /<td>.*?<\/td>.*?<td>(.*?)<\/td>/s; // flag s = dot matches new line
var m = response.match(pattern);
console.log(m[1]); // prints "1165"

这篇关于使用纯JavaScript获取HTML表中特定单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆