【问题标题】:PHP createTextNode with br tag带有 br 标签的 PHP createTextNode
【发布时间】:2016-04-29 01:02:27
【问题描述】:

我敢肯定,这比我做的要简单得多。 我要创建代码:

text<br/>

但是,您不能像使用段落一样使用 DOMNode::createElement 将节点文本添加到 br:

$doc->createElement('br',$para->nodeValue)

我也尝试过创建一个文本节点,然后向它添加中断。这不起作用!:

$newelement = $doc->createTextNode($para->nodeValue);
$newelement->appendChild($doc->createElement('br'));
$new_node = $para->parentNode->replaceChild($newelement, $oldelement);

任何人都可以建议在其后添加带有 br 标签的行的正确方法吗?

【问题讨论】:

  • 也许使用文档片段并附加到那个?

标签: php domdocument createelement createtextnode


【解决方案1】:
$frag=$doc->createDocumentFragment();
$br=$doc->createElement('br');
$txt=$doc->createTextNode($para->nodeValue);

$frag->appendChild( $br );
$frag->appendChild( $txt );

/* etc*/
$para->parentNode->appendChild( $frag );

【讨论】:

  • 谢谢。这确实解决了问题。非常感谢!
  • 欢迎,很高兴它有帮助。考虑接受答案,以便其他人知道该问题有答案 - 但取决于您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 2015-05-21
  • 2010-09-13
  • 2011-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多