如何使用开始和结束后的单词突出显示单词

时间:2017-07-25 10:36:59

标签: javascript php jquery wordpress

我正在使用下面的代码来获取和存储单词的开始和结束位置现在我想在刷新页面时突出显示这些单词..怎么做。

function getSelectionCharOffsetsWithin(element) 
{

var start = 0, end = 0;
var sel, range, priorRange;

if (typeof window.getSelection != "undefined") {
    range = window.getSelection().getRangeAt(0);
    priorRange = range.cloneRange();
    priorRange.selectNodeContents(element[0]);
    priorRange.setEnd(range.startContainer, range.startOffset);
    start = priorRange.toString().trim().length;
    end = start + range.toString().length;
} else if (typeof document.selection != "undefined" &&
    (sel = document.selection).type != "Control") {
    range = sel.createRange();
    priorRange = document.body.createTextRange();
    priorRange.moveToElementText(element);
    priorRange.setEndPoint("EndToStart", range);
    start = priorRange.text.length;
    end = start + range.text.length;
}
return {
    start: start,
    end: end
  };
}

0 个答案:

没有答案