开发者

How do I find a node in a range, and then remove it?

I'm copying a range from the page and pasting it back elsewhere, but this becomes problematic whe开发者_Python百科n it includes a piece of the page that I don't want to copy. Is there a good way to remove nodes from a range by id? Or if not, in the event that there are two nodes on the page with the same id, is there any way to identify one over the other (ie, via first specifying a unique parent id?)

Note: JavaScript only please - I can't use a library for this solution.


If you're talking about DOM Ranges, the solution is to split the Range around the node you want to exclude, giving you two Ranges.

// Assuming you have a Range stored in a variable called range and
// a node you want to exclude in a variable called node
var newRange = range.cloneRange();
range.setEndBefore(node);
newRange.setStartAfter(node);

This won't work in IE <= 8, which has an entirely different way of representing ranges.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜