【乘法表】innerHTML可以将字符串标签转为html标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var body = document.body;
var table = document.createElement('table');
table.setAttribute("border", 1);
var trs = '';
for(var i = 1; i < 10; i++) {
var tds = '';
for(var j=1; j <= i; j++) {
tds += '<td>' + i + '*' + j + '=' + i * j +'</td>';
}
trs += '<tr>' + tds + '</tr>';
}
table.innerHTML = trs;
body.append(table);
</script>
</body>
</html>
法二:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>99乘法表</title> <script type="text/javascript"> document.write("<center><table border=\"3\">"); for (i = 1; i <= 9; i++) { document.write("<tr>"); for (j = 1; j <= 9; j++) { if (j <= i) { document.write("<td>" + i + "*" + j + "=" + i * j + "</td>") } else { document.write("<td> </td>"); } } document.write("</tr>"); } document.write("</table></center>"); </script> </head> <body> </body> </html>