如何通过Javascript显示输入列表行? [英] How do I display input column table rows through Javascript?

查看:84
本文介绍了如何通过Javascript显示输入列表行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,该程序将为Table元素提供适当的列和行,而我能够执行行的一部分,但是卡在列上,我的代码不起作用?

I'm trying to write a program that will give my Table element appropriate column and rows I was able to do the rows part but I get stuck on columns, what is my code not working?

<body>
    <table>
        <script>
            var c = parseInt(prompt("Enter column ")) // 10
            var r = parseInt(prompt("Enter column ")) // 10

            while (0 < c) {
                c--;
                document.write("<td style></td>")
            }

            var r = parseInt(prompt("Enter row "))
            while (0 < r) {
                r--;
                document.write("<tr></tr>")
            }
        </script>
    </table>
</body>

推荐答案

列由单元格定义-单元格嵌套在行中.另外,您将需要多次(每行)执行一次,因此您不能花费该变量.

Columns are defined by the cells - which are nested in the rows. Also, You would need to run through that more than once (for each row), so you can't spend the variable.

尝试如下代码:

<script>
   var c = parseInt(prompt("Enter column ")) // 10
   var r = parseInt(prompt("Enter row ")) // 10
   var cTmp = c;

   while(0<r){
      r--;
      cTmp = c;
      document.write("<tr>")
      while(0<cTmp){
          cTmp--; 
          document.write("<td style></td>")
      }
      document.write("</tr>")
   }
</script>

修改

您应该注意html表不是由行和列定义的,而是由行以及每一行中的单元格定义的.本质上,这会创建列,但是我们为每一行重新定义它们.

You should note that html tables are not defined by rows and columns, rather by rows, and the cells within each row. In essence, this creates columns, but we redefine them for each row.

这篇关于如何通过Javascript显示输入列表行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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