首页 > 解决方案 > 将 .find 的目标从 tr:has(th) 更改为查找 div

问题描述

我正在使用(http://bl.ocks.org/kalebdf/ee7a5e7f44416b2116c0)中的代码将html表下载到csv。我用过其他人,但他们不能在 IE 上工作。上面链接中的那个确实适用于 IE。

我遇到的问题是我的表格是使用 div 中的列标题生成的 - 列标题

当我下载表格 ot csv 时,表格行数据包含在 csv 中,但缺少列标题。

我假设原因是因为它们在 div 中。js 文件的一部分为标题和行创建了一个变量。

    var $headers = $table.find('tr:has(th)'),
        $rows = $table.find('tr:has(td)')

我的问题是:如何更改 tr:has(th) 以便在 div 中找到文本?

标签: jquery

解决方案


假设标题divth...

喜欢:

<tr>
  <th>
    <div>blah</div>
  </th>
  <th>
    <div>blah-blah</div>
  </th>
</tr>

尝试将grabRow功能更改为:

function grabRow(i,row){

    var $row = $(row);
    //for some reason $cols = $row.find('td') || $row.find('th') won't work...
    var $cols = $row.find('td');
    if(!$cols.length) $cols = $row.find('div');  // Change th for div on this line

    return $cols.map(grabCol)
                .get().join(tmpColDelim);
}

推荐阅读