(我使用的是Bootstrap4。)我正在尝试在<span id="spanId" onload="numEntries()"></span>
我尝试了从iframe调用页面的方法:
<iframe src="A.html" style="display:none" id="iframeId"></iframe>
(来自getElementById from another page)
然后访问表的行数:
function numEntries() {
//call table element from other page through the iframe
var divElement = document.getElementById('iframeId').contentWindow.document.getElementById('#alarmTable');
//initialize the table
var num = $(divElement).DataTable();
document.getElementById("spanId").innerHTML = num.page.info().recordsTotal;
}
此方法未显示任何内容,我认为对iframe的调用存在问题。我还有一个用于表数据的jQuery页面:
$('#alarmTable').DataTable({
"data": [
["1", "2013-10-15 10:30:00", "2 min", "Alarm", "Motor 1", "Broken", "tag"],
["2", "2015-11-01 03:17:26", "8 min", "Warning", "Motor 2", "Stopped", "tag"]
]
});
此示例数据应显示2。如何显示表格中的行数?
答案 0 :(得分:0)
有很多错误,所以我将列举它们。
这导致解决方案达到预期的结果。
<script type="text/javascript">
function numEntries() {
var childWindow = document.getElementById('iframeId').contentWindow;
// Poll For Required Features in Child Window (iframe)
if (
!childWindow.$ ||
!childWindow.$(childWindow.document).DataTable
) {
setTimeout(numEntries,50);
return;
}
//call table element from other page through the iframe
var divElement = childWindow.document.getElementById('alarmTable');
//initialize the table
var num = childWindow.$(divElement).DataTable();
document.getElementById("spanId").innerHTML = num.page.info().recordsTotal;
}
$(document).ready(function() {
numEntries();
});
</script>