PHP DOMNode insertBefore(不允许修改错误)
我注意到,当尝试调用 DOMNode 的 insertBefore 方法时,其中要插入的节点来自另一个文档(即与插入的引用节点 和 节点不同),PHP 运行时生成 DOMException,其中消息为“不允许修改错误”。
尽管我确实看到一些提到插入的节点是只读,但关于这个问题的文档似乎很少。
我发现有效的解决方法是克隆来自不同文档的节点并插入克隆。示例:
foreach($nodeChildren as $child) {
$clone = $child->cloneNode(true);
$parentNode->insertBefore($clone, $nodeToInsertInFrontOf);
}
我的问题有两个:
1)为什么我必须克隆此节点才能执行插入?
2)这是执行此操作的最有效方法(假设克隆的子节点可能包含多个子节点和孙子节点的多个层次结构深度)?
I've noticed that when attempting to call a DOMNode's insertBefore method where the node to-be-inserted is from another document (i.e. different from the reference node and node being inserted into), the PHP run time generates a DOMException where the message is 'No Modification Allowed Error'.
Documentation seems to be sparse on this issue although I did see some mention of the node being inserted into is read only.
The workaround that I've found works is to clone the node that is from a different document and insert the clone. Example:
foreach($nodeChildren as $child) {
$clone = $child->cloneNode(true);
$parentNode->insertBefore($clone, $nodeToInsertInFrontOf);
}
My question is twofold:
1) Why do I have to clone this node in order to perform an insert?
2) Is this the most efficient way of performing this action (assuming that the cloned child node may contain several children and several levels of hierarchy deep of grandchildren)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据定义,DOM 中的对象只知道其自己文档中的对象。这是一个安全问题。
By definition, objects inside a DOM only know objects inside it's own document. It is a security thing.