为什么我运行代码时没有数字,方形和立方体显示?

时间:2015-11-30 10:32:19

标签: javascript web

我正在用书学习javascript,这是一个练习: 计算从0到10的数字的正方形和立方体,并在html表中显示值。 它们应该显示如下: Table

我的代码是:

<head>

<script type="text/javascript">
<!--
var square;
var cube;
document.write("<table border='0'>");
for (var number = 0; number <=10; number +=1){
    square = number*number;
    cube = number*number*number;    
    document.write("<tr>");
    document.write("<td>");
    document.writeln(number);
    document.write("</td>");
    document.write("<td>");
    document.writeln(square);
    document.write("</td>");
    document.write("<td>");
    document.writeln(cube);
    document.write("</td>");
    document.write("</tr>");
}   
document.write("</table>");         
// -->
</script>
</head>

问题是,当我运行这个时,我没有得到名为Number Square Cube的标题

我只得到数值结果......我的错误在哪里?

5 个答案:

答案 0 :(得分:1)

您可以生成类似

的表格
document.write("<table><tr><th>number</th><th>square</th><th>cube</th></tr>");
for(i=1;i<=10;i++){
var square=i*i;
var cube=i*i*i;
    document.write("<tr><td>"+i+"</td><td>"+square+"</td><td>"+cube+"</td></tr>");

}

答案 1 :(得分:0)

我不完全确定你要问的是什么,但是如果你想要文字,你还需要:

document.writeln('Number: ' + number);

此外还希望在using document.write

处获得好消息

答案 2 :(得分:0)

添加&lt; thead&gt; 部分,如下所示:

<head>

<script type="text/javascript">
<!--
var square;
var cube;
document.write("<table border='0'>");
document.write("<thead><th>number</th><th>square</th><th>cube</th></thead>");
for (var number = 0; number <=10; number +=1){
    square = number*number;
    cube = number*number*number;    
    document.write("<tr>");
    document.write("<td>");
    document.writeln(number);
    document.write("</td>");
    document.write("<td>");
    document.writeln(square);
    document.write("</td>");
    document.write("<td>");
    document.writeln(cube);
    document.write("</td>");
    document.write("</tr>");
}   
document.write("</table>");         
// -->
</script>
</head>

答案 3 :(得分:0)

您错过了标题行...

&#13;
&#13;
<head>

  <script type="text/javascript">
    <!--
    var square;
    var cube;
    document.write("<table border='0'>");
    document.write("<tr>");
    document.write("<td>");
    document.writeln("number");
    document.write("</td>");
    document.write("<td>");
    document.writeln("square");
    document.write("</td>");
    document.write("<td>");
    document.writeln("cube");
    document.write("</td>");
    document.write("</tr>");
    for (var number = 0; number <= 10; number += 1) {
      square = number * number;
      cube = number * number * number;
      document.write("<tr>");
      document.write("<td>");
      document.writeln(number);
      document.write("</td>");
      document.write("<td>");
      document.writeln(square);
      document.write("</td>");
      document.write("<td>");
      document.writeln(cube);
      document.write("</td>");
      document.write("</tr>");
    }
    document.write("</table>");
     // -->
  </script>
</head>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

<script type="text/javascript">
    <!--
    var square;
    var cube;
    document.write("<table border='0'>");
    //document.write("<td>")

    document.write("<tr>");
    document.write("<td>");
    document.writeln("number");
    document.write("</td>");
    document.write("<td>");
    document.writeln("Square");
    document.write("</td>");
    document.write("<td>");
    document.writeln("Cube");
    document.write("</td>");
    document.write("</tr>");

    for (var number = 0; number <=10; number +=1){
        square = number*number;
        cube = number*number*number;
        document.write("<tr>");
        document.write("<td>");
        document.writeln(number);
        document.write("</td>");
        document.write("<td>");
        document.writeln(square);
        document.write("</td>");
        document.write("<td>");
        document.writeln(cube);
        document.write("</td>");
        document.write("</tr>");
    }
    document.write("</table>");
    // -->
</script>

尝试使用此代码而不是代码。 在您的代码中,您错过了列的标题。