duanlieshuang5330 2016-10-15 11:45
浏览 58
已采纳

从“td”标签中的字符串中提取字符[关闭]

I have a table:

<tr>
    <td id="rk1">First RK</td>
    <td id="rk1_1">09231</td>
</tr>
<tr>
    <td id="rk2">Second RK</td>
    <td id="rk2_1">60234</td>
    <td id="rk2_2">69260</td>
</tr>
<tr>
    <td id="rk3">Third RK</td>
    <td id="rk3_1">30224</td>
    <td id="rk3_2">30302</td>
    <td id="rk3_3">20280</td>
    <td id="rk3_4">82122</td>
</tr>

And I want to extract content like this:

result1 = 23 //("rk1 / column 1 / character 3" and "rk2 / column 1 / character 4" )
result2 = 62 //("rk2 / column 2 / character 1" and "rk3 / column 3 / character 3")
result3 = 91 //("rk2 / column 2 / character 2" and "rk3 / column 4 / character 3")
...

Use array (condition) to extract string:

("rk1/1/3 and rk2/1/4", "rk2/2/1 and rk3/3/3", "rk2/2/2 and rk3/4/3",...)

How can i do this task?

  • 写回答

1条回答 默认 最新

  • dongzi9196 2016-10-15 12:03
    关注

    You could do it with the code below, but the way you format your parameters is kind of weird, and may produce errors if not formatted correctly.

    var parameters = ["rk1/1/3 and rk2/1/4", "rk2/2/1 and rk3/3/3", "rk2/2/2 and rk3/4/3"];
    
    // For each set of parameters, execute extract_content
    for (var i = 0; i < parameters.length; i++) {
      console.log('Result for "' + parameters[i] + '":');
      console.log(extract_content(parameters[i]));
    }
    
    function extract_content(params) {
      // Split selections, to get something like ["rk1/1/3", "rk2/1/4"]
      var selections = params.split(' and '),
          res = '';
      // For each selection
      for (var i = 0; i < selections.length; i++) {
        // Split it on slashes, to get ["rk1", "1", "3"]
        var parts = selections[i].split('/'),
            // Get the element with ID "rk1_1"
            elem = document.getElementById(parts[0] + '_' + parts[1]);
        // If the element exists
        if (elem) {
          // Add the character at position X to the result
          res += elem.textContent.trim()[parts[2] - 1];
        }
      }
      // Convert result to a number and return it
      return res;
    }
    <table>
      <tr>
        <td id="rk1">First RK</td>
        <td id="rk1_1">09231</td>
      </tr>
      <tr>
        <td id="rk2">Second RK</td>
        <td id="rk2_1">60234</td>
        <td id="rk2_2">69260</td>
      </tr>
      <tr>
        <td id="rk3">Third RK</td>
        <td id="rk3_1">30224</td>
        <td id="rk3_2">30302</td>
        <td id="rk3_3">20280</td>
        <td id="rk3_4">82122</td>
      </tr>
    </table>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测