开发者

PHP DOMNode insertBefore (No Modification Allowed Error)

开发者 https://www.devze.com 2023-02-07 03:11 出处:网络
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)

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 开发者_JAVA百科'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)?


By definition, objects inside a DOM only know objects inside it's own document. It is a security thing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号