开发者

DOM change element content

开发者 https://www.devze.com 2023-03-27 09:45 出处:网络
How does one change the element content with PHP DOM functions? In depth... I\'ve queried my element, modified attributes and now want to chang开发者_如何转开发e the content of it, how can I do this?

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

In depth... I've queried my element, modified attributes and now want to chang开发者_如何转开发e the content of it, how can I do this?


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

精彩评论

暂无评论...
验证码 换一张
取 消