简单的划词选中处理方法

    $('#click').click(function(){
        var selection = window.getSelection();
        console.log(selection);
        var range = selection.getRangeAt(0);
        console.log(range);
        var selectionContents = range.extractContents();
        var span = document.createElement("span");
        span.style.color = "red";
        span.appendChild(selectionContents);
        range.insertNode(span);
    });

 

 

点击按钮,选中的词变红

posted @ 2015-12-24 15:31  菩提明镜  阅读(137)  评论(0编辑  收藏  举报