【问题标题】:DOM change element contentDOM 改变元素内容
【发布时间】:2011-10-25 16:02:19
【问题描述】:

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

深入...我已经查询了我的元素,修改了属性,现在想更改它的内容,我该怎么做?

【问题讨论】:

    标签: php dom domdocument


    【解决方案1】:

    如果要设置元素的文本内容,请设置nodeValue属性:

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

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

    之类的操作
    $frag = $dom->createDocumentFragment();
    $frag->appendXML('<h1>foo</h1>');
    $el->appendChild($frag);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 2019-09-15
      • 2018-06-22
      • 2011-06-08
      • 1970-01-01
      相关资源
      最近更新 更多