DomDocument innerHTML 在简单的 php 脚本中不起作用


DomDocument innerHTML does not work in simple php script

innerHTML 在 php DomDocument 中不起作用,我无法通过 id 设置 html 元素的内部 html,没有错误/警告! 我该怎么办?

(我还检查了文件名和html元素ID)

    $index = new DomDocument;
    $index->validateOnParse = true;
    $index->formatOutput = true;

    $index->loadHTML('index.php');
    $index->getElementById('element-unique-id')->innerHTML = 'some text';

    echo $index->saveHTML();

输出为空。

由于没有inneHTML属性,您可以通过创建和附加一个新的DOMText节点来解决它:

$index->getElementById('element-unique-id')
      ->appendChild(new DOMText('some text'));

另请参阅 DOMText 类和 DOMText::__construct。