例//row=5 col=5
var str = "<table>"
var row = 1
while (row < 6) {
str += "<tr>"
var col = 1
while (col < 6) {
str += "<td>" + row + "</td>"
col++
}
str += "</tr>"
row++
}
str += "</table>"
document.body.innerHTML += str
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
td {
width: 20px
height: 10px
border: 1px solid #000
}
</style>
</head>
<body>
<script>
//乘法表
var str = "<table>"
var row = 1
while (row < 10) {
str += "<tr>"
var col = 1
//让后面的值小于等于前面得值
while (col <= row) {
str += "<td>" + row + "*" + col + "=" + row * col + "</td>"
col++
}
str += "</tr>"
row++
}
str += "</table>"
document.body.innerHTML += str
</script>
</body>
</html>
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var num=0
for(var i=1 i<10i++){
for(var j=1j<10j++){
num=i*j
if(num/10<1){
num="&nbsp&nbsp"+i*j
}
if(j<=i) {
document.write(j + "*" + i + "=" + num + "&nbsp&nbsp&nbsp&nbsp")
}
}
document.write("<br>")
}
</script>
</body>
</html>
扩展资料:
JavaScript For 循环
JavaScript 支持不同类型的循环:
for - 循环代码块一定的次数
for/in - 循环遍历对象的属性
while - 当指定的条件为 true 时循环指定的代码块
do/while - 同样当指定的条件为 true 时循环指定的代码块
下面是 for 循环的语法:
for (语句 1语句 2语句 3)
{
被执行的代码块
}
语句 1 在循环(代码块)开始前执行;
语句 2 定义运行循环(代码块)的条件;
语句 3 在循环(代码块)已被执行之后执行。
参考资料:百度百科-JavaScript