乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      javascript – 確定位于單元格下方的表列中的單元格

       印度阿三17 2019-07-10

      在下表中:

      <table>
          <thead>
              <tr>
                  <th>Th1</th>
                  <th colspan='2'>Th23</th>
                  <th>Th4</th>
              </tr>
          </thead>
          <tbody>
              <tr>
                  <td>Td1</td>
                  <td>Td2</td>
                  <td>Td3</td>
                  <td>Td4</td>
             </tr>
          </tbody>
      </table>
      

      對于包含文本“Th23”的表格單元格,我想知道它下面有哪些單元格.在這種情況下,答案將是分別包含文本“Td2”和“Td3”的單元格.

      是否有任何DOM屬性或內(nèi)置函數(shù)可以幫助進行此類計算?

      @Matt McDonald有一個更通用的解決方案.

      這就是我最終得到的結(jié)果:

      // get tbody cell(s) under thead cell (first arg)
      // if rowIndex===undefined, get from all rows; otherwise, only that row index
      // NOTE: does NOT work if any cell.rowSpan != 1
      var columnCells = function( th, rowIndex ) {
          // get absolute column for th
          for( var absCol=0, i=0; true; i   ) {
                  if( th.parentNode.cells[i] == th ) break;
                  absCol  = th.parentNode.cells[i].colSpan;
          }
          // look in tBody for cells; all rows or rowIndex
          var tBody = th.parentNode.parentNode.nextSibling;
          var cells = [];
          for( var r=((rowIndex==undefined)?0:rowIndex); true; r   ) {
                  if( rowIndex!==undefined && r>rowIndex ) break;
                  if( rowIndex==undefined && r>=tBody.rows.length ) break;
                  for( var c=0; true; c =tBody.rows[r].cells[c].colSpan ) {
                          if( c < absCol ) continue;
                          if( c >= absCol th.colSpan ) break;
                          cells.push(tBody.rows[r].cells[c]);
                  }
          }
          return cells;
      }
      

      解決方法:

      馬上,你需要做三件事:

      >為表提供id屬性以便于選擇.
      >為目標單元格提供id屬性以便于選擇.
      >選擇單元格的parentNode(行)

      這三件事將使表格相關(guān)的計算更容易.

      Next up是一個抓取指定單元格的偽屬性的函數(shù).在這種情況下,我們正在尋找它的“起始索引”(就列而言),它的“結(jié)束索引”(就列而言),以及它的“寬度”(結(jié)束 – 開始,也在列中).

      從那里,您可以遍歷表的行并檢查哪些單元格位于開始和結(jié)束索引之間.

      HTML:

      <table id="foo">
          <colgroup span="1">
          <colgroup span="2">
          <colgroup span="1">
          <thead>
              <tr>
                  <th>foo</th>
                  <th id="example" colspan="2">bar</th>
                  <th>baz</th>
              </tr>
          </thead>
          <tbody>
              <tr>
                  <td>bing</td>
                  <td>bang</td>
                  <td>boom</td>
                  <td>bong</td>
              </tr>
          </tbody>
      </table>
      

      JS(跟我一起):

      function getCellSpanProps(table, row, cell)
      {
          var isRow = (function()
          {
              var i = 0, currentRow;
              for(i;i<table.rows.length;i  )
              {
                  currentRow = table.rows[i];
                  if(currentRow === row)
                  {
                      return true;
                  }
                  currentRow = null;
              }
              return false;
          }()), 
          cellHasCorrectParent, i = 0, 
          currentCell, colspanCount = 0,
          props;
          if(isRow)
          {
              cellHasCorrectParent = (function()
              {
                  return cell.parentNode === row;
              }());
              if(cellHasCorrectParent)
              {
                  for(i;i<row.cells.length;i  )
                  {
                      currentCell = row.cells[i];
                      if(currentCell === cell)
                      {
                          props = {"start": colspanCount, 
                          "end": colspanCount   cell.colSpan, 
                          "width": (colspanCount   cell.colSpan) - colspanCount};
                          break;
                      }
                      colspanCount  = currentCell.colSpan;
                      currentCell = null;
                  }
                  row = null;
              }
              return props;
          }
      }
      
      function findCellsUnderColumn(table, props)
      {
          var i = 0, j = 0, row, cell,
          colspanCount = 0, matches = [],
          blacklist = {"": true, "NaN": true, "null": true, "undefined": true, 
          "false": true};
          if(blacklist[props.start] || blacklist[props.end] || blacklist[props.width])
          {
              return false;
          }
          for(i;i<table.rows.length;i  )
          {
              row = table.rows[i];
              colspanCount = 0;
              for(j=0;j<row.cells.length;j  )
              {
                  cell = row.cells[j];
                  if(colspanCount >= props.start && colspanCount < props.end)
                  {
                      matches.push(cell);
                  }
                  colspanCount  = cell.colSpan;
                  cell = null;
              }
              row = null;
          }
          return matches;
      }
      
      var table = document.getElementById("foo"), 
      example = document.getElementById("example"),
      targetRow = example.parentNode,
      props = getCellSpanProps(table, targetRow, example),
      matches = findCellsUnderColumn(table, props);
      console.log(matches);
      

      演示:http:///ohohew/edit#javascript,html

      這將確定哪些單元格位于您要查找的特定列中(包括示例).您可以自定義功能以滿足您的需求,如果這不是您正在尋找的.

      來源:https://www./content-1-313051.html

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多