【问题标题】:AppendChild($element) to an existing html element with php使用 php AppendChild($element) 到现有的 html 元素
【发布时间】:2014-01-03 14:29:44
【问题描述】:

我正在使用以下 PHP 代码尝试将表单数据附加到 html 表中(HTML 文件已作为 Customers.html 存在)。

<?php
  $myfile = 'Customers.html';
  $myfile = realpath($myfile);
  $doc = new DOMDocument('1.0');
  $doc->loadHTML($myfile);
  $doc->formatOutput = True;

  $table = $doc->appendChild($doc->createElement('table'));
  $tableRow = $table->appendChild($doc->createElement("tr"));
  $userName = $tableRow->appendChild($doc->createElement('th'));
  $doc->saveHTMLFile("Customers.html");
?>

代码执行得很好,但它会将生成的 html 代码转储到 body 标记之外。我希望将由 PHP 创建的 table 标记作为 body 标记的子标记,但结果是这样的:

<html>
  <body>
  </body>
</html>
<table>
  <tr>
    <th>
    </th>
  </tr>
</table>

我想要的是这个:

<html>
  <body>
    <table>
       <tr>
         <th>
         </th>
       </tr>
    </table>
  </body>
</html>

我如何实现它?

我不想用php创建body标签。

我尝试使用

$body = $doc->getElementsByTagName('body');
$table = $doc->createElement('table');
$body->appendChild($table);

然后将 $table 附加到它上面,它不起作用。

谁能告诉我我做错了什么。

我是 PHP 新手,刚刚学习。 请告诉我是否需要更多信息。 任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 你能展示一下如何将表格附加到它上面吗?
  • 我已经添加了“我如何附加”

标签: php html


【解决方案1】:

试试这个

$body = $doc->getElementsByTagName('body')->item(0);
$table = $doc->createElement('table');
$body->appendChild($table);

【讨论】:

  • :( 现在它覆盖了之前制作的 $tables。我能做些什么来阻止它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 2012-05-08
  • 2012-05-12
  • 2018-04-12
相关资源
最近更新 更多