在Javascript中使用for循环创建表 [英] Creating table using for loop in Javascript

查看:151
本文介绍了在Javascript中使用for循环创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在使用Javascript的HTML文档中创建一个简单的时间表。这是我的代码:

 <!DOCTYPE> 
< html>
< head>
< title>表< / title>
< / head>
< body>
< script language =javascripttype =text / javascript>
for(var a = 0; a< 10; a ++){
document.write(< tr>);
for(var b = 0; b <10; b ++){
document.write(< td>a * b< / td>);
}
document.write(< / tr>);
}
< / script>
< / body>
< / html>

我查看了发布的问题,但找不到答案,可能是因为我是初学程序员并不明白大部分。
解决方案

那么,首先你应该插入tr(rows)和td(cells )转换为表元素...类似于

  document.write(< table>); 
//你的循环在这里
document.write(< / table>);

有更好的方法可以做到这一点!


I am trying to create a simple times table in a html document using Javascript. This is my code so far:

<!DOCTYPE>
    <html>
    <head>
        <title>Table</title>
    </head>
    <body>
    <script language="javascript" type="text/javascript">
        for (var a=0; a < 10; a++) {
            document.write("<tr>");
            for(var b=0; b<10; b++) {
                document.write("<td>"a*b"</td>");
        }
        document.write("</tr>");
        }
    </script>
    </body>
    </html>

I looked through the posted questions, but could not find an answer, possibly because I am a beginner programmer and did not understand most of it.

解决方案

Well, first of all you should insert the tr (rows) and td (cells) into a table element... Something like

document.write("<table>");
// your loop here
document.write("</table>");

There are better ways to do this, though!

这篇关于在Javascript中使用for循环创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆