[英]How to append source html to a DOMElement in PHP?
Is there a way of appending source html into a DOMElement? 有没有办法将源html附加到DOMElement? Something like this: 像这样的东西:
$trElement->appendSource("<a href='?select_user=4'>Username</a>");
It would parse that fragment and then append it. 它将解析该片段然后追加它。
You are looking for 你在找
- DOMDocumentFragment::appendXML
— Append raw XML data - DOMDocumentFragment::appendXML
- 附加原始XML数据
Example from Manual: 手册示例:
$doc = new DOMDocument();
$doc->loadXML("<root/>");
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>");
$doc->documentElement->appendChild($f);
echo $doc->saveXML();
If you don't have a reference to the document root in scope, you can always access it via the ownerDocument
property of an arbitrary node: 如果在作用域中没有对文档根的引用,则始终可以通过任意节点的ownerDocument
属性访问它:
$frag = $trElement->ownerDocument->createDocumentFragment();
$frag->appendXML("<a href='?select_user=4'>Username</a>");
$trElement->appendChild($frag);
Yes, you can do this with DOMDocument::createDocumentFragment
: 是的,您可以使用DOMDocument::createDocumentFragment
执行此操作:
$fragment = $dom->createDocumentFragment();
$fragment->appendXML('<a href="select_user=4">Username</a>');
$element->appendChild($fragment);
In this case, it would be simpler to do it with a normal createElement
call: 在这种情况下,使用普通的createElement
调用更简单:
$el = $dom->createElement('a', 'Username');
$el->setAttribute('href', 'select_user=4');
$element->appendChild($el);
In each case, $element
is the DOM element to which you want to append your code. 在每种情况下, $element
都是您想要附加代码的DOM元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.