我正在尝试使用 JavaScript 在 HTML 中打印表格。当我尝试打印表格时,它只是给我一个空白页。这是我的 HTML 代码:
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script type="text/javascript" src="gen.js"></script>
<link rel="stylesheet" href = "index.css">
</head>
<body onload="print()">
<div id="tt"></div>
</body>
</html>
这是我的 JavaScript 文件:
function print(){
// console.log("?")
var time = Number(localStorage.getItem("time"))
// console.log(typeof time)
var nClass = Number(localStorage.getItem("nClass"))
var duration = localStorage.getItem("duration")
var subjectsRaw = localStorage.getItem("arr")
var subjects = JSON.parse(subjectsRaw)
document.getElementById('tt').innerHTML = "<table border = 1>"
for(var i = 0; i < 5; i++){
console.log('test')
document.getElementById('tt').innerHTML = "<tr>"
for (let j = 0; j < nClass; j++) {
console.log('test2')
document.getElementById('tt').innerHTML = '<td>' + subjects[Math.floor(Math.random() * subjects.length + 0)] + '</td>'
}
document.getElementById('tt').innerHTML = "</tr>"
document.getElementById('tt').innerHTML = "<br>"
}
document.getElementById('tt').innerHTML = "</table>"
编辑:现在使用 DOM 打印。
缥缈止盈
相关分类