我有一个脚本,可以改变已选择文本的背景颜色。但是,当我在跨多个元素/标签选择文本时遇到问题。getSelection&surroundContent跨多个标签
,我已经得到的代码是:
var text = window.getSelection().getRangeAt(0);
var colour = document.createElement("hlight");
colour.style.backgroundColor = "Yellow";
text.surroundContents(colour);
和错误是输出为:
Error: The boundary-points of a range does not meet specific requirements. =
NS_ERROR_DOM_RANGE_BAD_BOUNDARYPOINTS_ERR
Line: 7
我相信这是做的getRange()函数虽然我不太确定如何继续,因为我是一名JavaScript初学者。
有没有其他方法可以复制我想要实现的功能?
非常感谢。
重复http://stackoverflow.com/questions/2582831/highlight-the-text-of-the-dom-range-element和http://stackoverflow.com/questions/1622629/javascript-highlight-selected -range-button – 2010-04-06 11:18:05