使用 javascript 创建元素

2024-01-17

我有一个 html 表格

<table id = "rpttable" name = "rpttable">
  <thead>
    Column Headers here...
  </thead>
  <tbody id = "rptbody" name = "rptbody">
    data here <3 ....
  </tbody>
</table>

这是我的 php (sample.php)

<?php
  Query Code here..
  Query Code there..
  and so on

  //this is the way I populate a table
  while (query rows) {
    echo '<tr>';
    echo '<td>Sample Data</td>';
    echo '</tr>;
  }
?>

因此,为了使这项工作正常进行并填充表格,这就是我所做的。

<table id = "rpttable" name = "rpttable">
  <thead>
    Column Headers here...
  </thead>
  <tbody id = "rptbody" name = "rptbody">
    <?php
       include 'folder_location/sample.php';
    ?>
  </tbody>
</table>

忽略输出的图像,但是当我去Inspect Element甚至Ctrl + u我现在会看到我的表结构是这样的。

<table id = "rpttable" name = "rpttable">
  <thead>
    Column Headers here...
  </thead>
  <tbody id = "rptbody" name = "rptbody">
    <tr>
    <td>Sample Data</td>
    </tr>
  </tbody>
</table>

现在事情是这样的。我不这样做,这就是我所做的。

$("#rpttable tr").remove();
        for (i = 0; i < data.length; i++) {
            tr = $("<tr />");
            for (x in data[i]) {
                td = $("<td />");
                td.html(data[i][x]);
                tr.append(td);
            }
            rpttable.append(tr);                    
        }

相同的输出它确实填充了表格,但是当我去Inspect Element甚至Ctrl + u输出是。

<table id = "rpttable" name = "rpttable">
  <thead>
    Column Headers here...
  </thead>
  <tbody id = "rptbody" name = "rptbody">
    **This is the part missing**
  </tbody>
</table>

我的问题是如何使用 javascript/ajax 创建一个元素? php 中的输出相同。我的意思是写元素。

** 更新 ** 我正在尝试从外部文件运行一个 css 类,如果我手动编辑它以满足我的需要,我将花费很长时间,而且我很难解释它是一个表类。我尝试使用该类使用默认值<table>。你知道在后端手动编写它。现在我尝试使用 php 和 ajax 来填充它,到目前为止,它确实填充了,但是当我尝试运行该类时,该类不起作用。

TYSM


使用 jquery,您可以使用以下方法将 html 行添加到 tbody:

$("#rptbody").html("<tr><td>value</td></tr>");

这是你想做的吗?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 javascript 创建元素 的相关文章