Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

Get value of a specific cell in HTML table with pure JavaScript

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>

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>).

The method looks like this:

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...