使用jQuery添加到附加表行的链接

时间:2014-02-03 19:38:44

标签: javascript jquery html

使用jQuery,我将行追加到表中,每行包含一个锚标记。然后,jQuery应添加到锚标记的链接,但链接不会显示。其他数据(例如,日期,地点)填写正常,所以我不确定问题是什么。

jQuery的:

for (var i = 1; i <= 20; i++) {
  ...
  $('.recent-reports tbody').append('<tr><td></td><td></td><td><a class="js-pdf-download" href="">Download</a></td></tr>');
  $('.recent-reports tbody tr:last').find('td').eq(0).text(date)
                                    .find('td').eq(1).text(location)
                                    .find('.js-pdf-download').attr("href", link)
  ...    
}

HTML:

<table class="recent-reports">
    <thead>
    </thead>
    <tbody>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

感谢Johann在同时简化我的代码的同时找到了解决方法。而不是在表中添加一行,然后然后添加数据,我现在一步追加数据。

$('.recent-reports tbody').append('<tr><td>' + date + '</td><td>' + location + '</td><td><a class="js-pdf-download" href="' + link + '" target="_blank">Download</a></td></tr>')

此外,一个笨拙的解决方案,但一个本来符合我原来的方法:

 ...
  $('.recent-reports tbody tr:last').find('td').eq(0).text(date)
  $('.recent-reports tbody tr:last').find('td').eq(1).text(location)
  $('.recent-reports tbody tr:last').find('.js-pdf-download').attr("href", link)
  ...