如何使用PHP DOM函数更改元素内容?
深入的..。我已经查询了我的元素,修改了属性,现在想要更改它的内容,我该怎么做呢?
发布于 2011-08-12 17:28:18
如果要设置元素的文本内容,请设置nodeValue
属性:
$el = $dom->getElementById('foo');
$el->nodeValue = 'hello world';
注意,这会自动转义<
和>
,所以您不能像这样插入HTML。为此,您必须执行类似于DOMDocument::createDocumentFragment
的操作
$frag = $dom->createDocumentFragment();
$frag->appendXML('<h1>foo</h1>');
$el->appendChild($frag);
https://stackoverflow.com/questions/7038065
复制相似问题