DOM更改元素内容 [英] DOM change element content

查看:79
本文介绍了DOM更改元素内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用PHP DOM函数更改元素内容?

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, how can I do this?

推荐答案

设置 nodeValue 属性:

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

请注意,这会自动转义<>,因此您不能插入这样的HTML.为此,您必须执行 DOMDocument::createDocumentFragment 这样的操作:

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);

这篇关于DOM更改元素内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆