DOM change element content

前端 未结 1 1725
星月不相逢
星月不相逢 2020-12-31 12:47

How does one change the element content with PHP DOM functions?

In depth... I\'ve queried my element, modified attributes and now want to change the content of it, h

相关标签:
1条回答
  • 2020-12-31 13:32

    Set the nodeValue property if you want to set text content of an element:

    $el = $dom->getElementById('foo');
    $el->nodeValue = 'hello world';
    

    Note that this automatically escapes < and >, so you can't insert HTML like this. For that you'll have to do something like DOMDocument::createDocumentFragment:

    $frag = $dom->createDocumentFragment();
    $frag->appendXML('<h1>foo</h1>');
    $el->appendChild($frag);
    
    0 讨论(0)
提交回复
热议问题