<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
table {
border-collapse: collapse;
}
</style>
</head>
<body>
<table border=1px>
</table>
<script>
let table = document.querySelector(‘table‘);
//第一种
for (let i = 1; i <= 9; i++) {
let tr = document.createElement(‘tr‘);
for (let j = 1; j <= i; j++) {
let td = document.createElement(‘td‘);
td.innerText = ` ${i}*${j}=${i * j}`;
tr.appendChild(td);
}
table.appendChild(tr);
}
//第二种
let tableHTML = ‘‘;
for (let i = 1; i <= 9; i++) {
tableHTML += ‘<tr>‘;
for (let j = 1; j <= i; j++) {
tableHTML += `<td>${i}*${j}=${i * j}</td>`
}
tableHTML += `<tr>`;
}
table.innerHTML = tableHTML;
</script>
</body>
</html>
原文地址:https://www.cnblogs.com/James23/p/9129357.html
时间: 2024-10-11 23:27:44