【发布时间】:2011-10-25 16:02:19
【问题描述】:
如何使用 PHP DOM 函数更改元素内容?
深入...我已经查询了我的元素,修改了属性,现在想更改它的内容,我该怎么做?
【问题讨论】:
标签: php dom domdocument
如何使用 PHP DOM 函数更改元素内容?
深入...我已经查询了我的元素,修改了属性,现在想更改它的内容,我该怎么做?
【问题讨论】:
标签: php dom domdocument
如果要设置元素的文本内容,请设置nodeValue属性:
$el = $dom->getElementById('foo');
$el->nodeValue = 'hello world';
请注意,这会自动转义 < 和 >,因此您不能像这样插入 HTML。为此,您必须执行 DOMDocument::createDocumentFragment:
$frag = $dom->createDocumentFragment();
$frag->appendXML('<h1>foo</h1>');
$el->appendChild($frag);
【讨论】: