【发布时间】:2022-01-22 11:37:31
【问题描述】:
我正在寻求使用 DOMDocument 构建一个 HTML 表。例如,表格的第一行(标题行)如下所示:
<tr><th>text</th><th>text</th><th>text</th><th>text</th><th>text</th><th>text</th></tr>
表行 (
我尝试了以下看起来合乎逻辑的方法:
$dom_obj = new DOMDocument('1.0', 'UTF-8');
// First the initial table tag.
$table = $dom_obj->createElement('table');
$dom_obj->appendChild($table);
// Table row
$tr = $dom_obj->createElement('tr');
$table->appendChild($tr);
// For loop ??
$th = $dom_obj->createElement('th');
$tr->appendChild($th);
$th = $dom_obj->createElement('th');
$th->appendChild($th);
$html_out = $dom_obj->saveHTML();
我收到以下错误消息:
PHP 致命错误:未捕获的 DOMException:层次结构请求错误。 . . . . . .
据我所知,这意味着 DOMDocument 不喜欢我试图将类似的元素附加在一起。
我还尝试了以下方法,在我去的时候唯一地命名了
$dom_obj = new DOMDocument('1.0', 'UTF-8');
// First the initial table tag.
$table = $dom_obj->createElement('table');
$dom_obj->appendChild($table);
// Table row
$tr = $dom_obj->createElement('tr');
$table->appendChild($tr);
// For loop ??
$th1 = $dom_obj->createElement('th');
$tr->appendChild($th1);
$th2 = $dom_obj->createElement('th');
$th1->appendChild($th2);
$html_out = $dom_obj->saveHTML();
这给出了以下结果:
仍然不是所需要的。
如何使用 PHP DOMDocument 将 【问题讨论】: 或
链接在一起?
$th 附加到自身,使用 $th->appendChild($th);,这没有意义。您可能打错字了,打算像前面几行一样写$tr->appendChild($th);。
标签: php html dom domdocument