【问题标题】:this function highlight selected text, how can i delete the span made by javascript clicking the highlighted text?此功能突出显示选定的文本,我如何删除 javascript 单击突出显示的文本所产生的跨度?
【发布时间】:2012-01-02 12:04:42
【问题描述】:

我找到了这个 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);
    }

【问题讨论】:

  • 你为什么发布重复的问题?
  • 因为我不想将背景更改为透明,我想删除在我的函数中制作背景的跨度
  • 你当时为什么接受这个答案?
  • 对不起,我是新来的,塞尔吉奥,如果你能帮我解决我的问题,谢谢
  • 看我的cmets到原来的问题

标签: javascript highlight html


【解决方案1】:
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);
}

See demo

【讨论】:

    猜你喜欢
    • 2011-08-31
    • 1970-01-01
    • 2020-11-12
    • 2012-06-23
    • 2011-12-25
    • 1970-01-01
    • 2018-02-06
    • 2012-03-28
    • 1970-01-01
    相关资源
    最近更新 更多