如何根据表格的宽度(而不是整个页面的宽度)将标题置于表格上方?

时间:2015-07-20 03:40:37

标签: javascript html css

所以这是我的代码,但我不知道要添加什么来使它根据表格宽度使文本居中。

<script type="text/javascript">
        "use strict";

        var principal;
        var rate;
        var numyears;
        var yearlist;
        var simpleinterest;
        var compoundinterest;

        yearlist = 1;
        principal = Number(prompt("Enter Principal"));
        rate = Number(prompt("Enter rate (percentage value)"));
        numyears = Number(prompt("Enter number of years"));

        document.writeln("<h1>Table for $"  + principal + " at " + rate + "%</h1>");
        document.writeln("<table id=\"interest\"><th>Year</th><th>SimpleInterest</th><th>CompoundInterest</th>");

        while (yearlist <= numyears) {
            compoundinterest = Math.pow((1+rate/100), yearlist) * principal;
            simpleinterest = (principal * yearlist * rate/100) + principal;

            document.writeln("<tr><td>" + yearlist + "</td><td>$" + simpleinterest + "</td><td>$" + compoundinterest + "</td></tr>");

            yearlist++;
        }
    </script>

1 个答案:

答案 0 :(得分:1)

你的问题不明确。但是猜测一下,我创造了这个

  <script type="text/javascript">
  "use strict";

    var principal;
    var rate;
    var numyears;
    var yearlist;
    var simpleinterest;
    var compoundinterest;

    yearlist = 1;
    principal = Number(prompt("Enter Principal"));
    rate = Number(prompt("Enter rate (percentage value)"));
    numyears = Number(prompt("Enter number of years"));     
    document.writeln("<table id=\"interest\">");

    document.writeln("<tr><td colspan='3' align='center'><h1 align='center'>Table for $"  + principal + " at " + rate + "%</h1></td></tr><th>Year</th><th>SimpleInterest</th><th>CompoundInterest</th>");

    while (yearlist <= numyears) {
        compoundinterest = Math.pow((1+rate/100), yearlist) * principal;
        simpleinterest = (principal * yearlist * rate/100) + principal;

        document.writeln("<tr><td>" + yearlist + "</td><td>$" + simpleinterest + "</td><td>$" + compoundinterest + "</td></tr>");

        yearlist++;
    }
</script>