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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,