如何突出显示
字样小提琴here
代码:
AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {
//use text ranges for Internet Explorer
debugger
var range = document.createRange();
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
var oRange = selection;
oRange.moveStart("character", iStart);
oRange.moveEnd("character", iLength);
oRange.select();
//set focus back to the textbox
this.textbox.focus();
};
但它在moveEnd
和moveStart
函数
答案 0 :(得分:1)
使用这个简单的功能
function hilighter(word, element) {
var rgxp = new RegExp(word, 'g');
var repl = '<span class="HiLighterClass">' + word + '</span>';
element.innerHTML = element.innerHTML.replace(rgxp, repl);
}
使用强>
你可以通过不同的方式获取元素,例如Id,Class等
hilighter('youWord', document.getElementByTag("Body"));