You will need to wrap the text that you want to in it's own <span>
tag so you can give that text its own style. Using your requested function definition, you could do it like this:
function (element, start, end) {
var str = element.innerHTML;
str = str.substr(0, start) +
'<span class="hilite">' +
str.substr(start, end - start + 1) +
'</span>' +
str.substr(end + 1);
element.innerHTML = str;
}
You can then define CSS for the class hilite to control the style of that text.
.hilite {color: yellow;}
This assumes that start and end are indexes into the innerHTML of the first and last characters that you want highlighted.
If you want to be able to call it repeatedly on the same element (to move the higlight around), you could do it like this:
function (element, start, end) {
var item = $(element);
var str = item.data("origHTML");
if (!str) {
str = item.html();
item.data("origHTML", str);
}
str = str.substr(0, start) +
'<span class="hilite">' +
str.substr(start, end - start + 1) +
'</span>' +
str.substr(end + 1);
item.html(str);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…