问题
I am making a clone of a DOM Range using cloneRange() function. If I then modify the original range object like this:
range.setStart(range.startContainer, 1);
The clone preserves the old startOffset
as expected.
However, if I modify the DOM tree, then the clone's startOffset
will also get affected. Is there an intentional internal wiring between the DOM tree and all ranges that are associated with it (clones included)?
JS Fiddle Example
回答1:
Yes, there is. What happens to ranges under DOM mutation is specified in the DOM Level 2 Range specification. The more recent DOM4 Range specification specifies this under the mutation algorithms section (thanks to RobG for pointing that out).
来源:https://stackoverflow.com/questions/11404787/dom-range-clones-are-not-immune-to-dom-changes