我发现此javascript代码突出显示所选文字,如何添加功能以删除突出显示的背景(删除已创建的范围)只需点击突出显示的文字?
highlight=function()
{
var selection= window.getSelection().getRangeAt(0);
var selectedText = selection.extractContents();
var span= document.createElement("span");
span.style.backgroundColor = "yellow";
span.appendChild(selectedText);
selection.insertNode(span);
}
答案 0 :(得分:3)
window.highlight = function() {
var selection = window.getSelection().getRangeAt(0);
var selectedText = selection.extractContents();
var span = document.createElement("span");
span.style.backgroundColor = "yellow";
span.appendChild(selectedText);
span.onclick = function (ev) {
this.parentNode.insertBefore(
document.createTextNode(this.innerHTML),
this
);
this.parentNode.removeChild(this);
}
selection.insertNode(span);
}