不显示表

Table not displayed

本文关键字:显示      更新时间:2023-09-26

我想按照我在下面的代码中的方式显示表格,但没有显示在屏幕上,代码只使用 html 和 Javascript

<body id="page">
 <center>
    <table id="tables" border="1px" cellspacing="50px" style="margin-top:70px" >
    <script type="text/javascript">
     var row=2;
        for(var j=1;j<=2;j++){          
            $('#tables').append("<tr id='row"+j+"' style='margin-bottom:50px'>");
            for(var k=1;k<=5;k++){
                $("#row"+j).append("<td id='table"+k+"' class='button'  width='150' height='150' onclick='printBill("+k+")' background='images/green.png' align='center'>"+
                                            "<font size='+8'><b>"+k+"</b></font>"+
                                            "<br><font id='clock"+k+"' size='+1'> 0:0:0 </font>"+
                                        "</td>");
                }
            $('#tables').append("<tr id='row"+j+"' style='margin-bottom:50px'>");
        }
    </script>
    </table>
</center>
</body>

任何人都可以帮助找出此代码的问题吗?

您可以使用两种典型的方法来修改页面加载的文档:

当 html 仍在加载时:

<table id="tables">
  <tbody>
    <script type="text/javascript">
      document.write('<tr>......</tr>');
    </script>
  </tbody>
</table>

加载 html 页面后(更具体地说,当 DOM 准备就绪时):

<table id="tables">
  <tbody>
  </tbody>
</table>
<script type="text/javascript">
  $(function(){ // see http://api.jquery.com/jQuery/#jQuery3
    // the full DOM is ready now, you can start modifying it.
    $('#tables > tbody').append('<tr>......</tr>');
  });
</script>

如您所见,我在table及其tr之间插入了一个tbody标签:浏览器通常会创建此tbody标签,即使您的源不包含它。通过始终添加tbody(如果您愿意,也可以添加thead/tfoot),您可以避免一些潜在的麻烦。

例如,请参阅我可以依赖"tbody"标签的隐式创建吗?或搜索implicit tbody

我清理了一下你的代码:http://jsfiddle.net/6332Q/1/