简单的划词选中处理方法
$('#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); });
点击按钮,选中的词变红