選択したテキストをスパン内に配置して、CSSで背景色を与えるこのコードがあります。2つの異なるdivからのテキスト、または任意の2つのタグ内にあるテキストを「マーク」しようとすると、エラーが発生します。
Uncaught Error: BAD_BOUNDARYPOINTS_ERR: DOM Range Exception 1
これは私のコードです:
function highlightSelection() {
var selection;
//Get the selected stuff
if(window.getSelection)
selection = window.getSelection();
else if(typeof document.selection!="undefined")
selection = document.selection;
//Get a the selected content, in a range object
var range = selection.getRangeAt(0);
//If the range spans some text, and inside a tag, set its css class.
if(range && !selection.isCollapsed)
{
var span = document.createElement('span');
span.className = 'highlight-green';
range.surroundContents(span);
}
}
HighlightSelection()はonmouseup
イベントとともに呼び出されます。よろしくお願いします!