我的页面上有许多表格,我需要在文档就绪函数中访问数据属性,如下所示,我还会显示HTML。
$('.accountsHoldingTable').DataTable({
dom: 'tB',
responsive: true,
"lengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
buttons: [
{
extend: 'excelHtml5',
filename: "Positions " + $(this).data("ttitle") + " " + today.toDateString()
}
],
});
<table id = "accountsHoldingTable"
data-ttitle = "my file name"
class = "headerSet table table-bordered display hover no-wrap dataTables accountsHoldingTable"
style = "width: 100%">
然而,这给了我
的结果“职位未定义2017年12月4日星期一(2).xlsx”
如何正确执行此操作,以便将undefined
替换为data-ttitle
?
答案 0 :(得分:1)
我猜你在错误的上下文中使用this
。看看这段代码是否能完成你想要实现的目标:
$('.accountsHoldingTable').each(function(i, val) {
$(this).DataTable({
dom: 'tB',
responsive: true,
lengthMenu: [[25, 50, 100, -1], [25, 50, 100, "All"]],
buttons: [
{
extend: 'excelHtml5',
filename: "Positions " + $(this).data("ttitle") + " " + today.toDateString()
}
]
});
});