javascript做九九乘法表,满满的干货!!!
基本代码:
<script type="text/javascript">var factorOne , factorTwo;var arr = new Array();//创建数组存储结果for (factorOne = 1; factorOne <= 9; factorOne++) {
//这里是行for (factorTwo = 1; factorTwo <= factorOne; factorTwo++) {
//这里是列arr[factorTwo - 1] = factorOne * factorTwo;}//由于数组是从零开始,所以要减一document.write(arr,'</br>');//逐行打印}</script>
可以得到下图结果:
基本功能已经实现但是这样也太丑了
所以我们优化一下代码:
添加css:
td {
width: 125px;height: 25px;line-height: 25px;border: 1px solid #000000;text-align: center;
}
js部分:
<script type="text/javascript">var factorOne , factorTwo;document.write("<table align='center'>")//利用write在文档里创建table表格for (factorOne = 1; factorOne <= 9; factorOne++) {
//这里是行document.write("<tr>");//创建行for (factorTwo = 1; factorTwo <= factorOne; factorTwo++) {
//这里是列document.write("<td>");//创建列document.write(factorOne+" × "+factorTwo+" = "+ (factorOne*factorTwo));//输出结果document.write("</td>");//在一列中创建多个表格,}document.write("</tr>");}document.write("</table>");</script>
结果为:
现在是不是神清气爽了许多~~~~