window.getSelection和document.selection
返回一个 Selection 对象,表示用户选择的文本。
IE9以下支持:document.selection
IE9、Firefox、Safari、Chrome和Opera支持:window.getSelection()
alert 选中的内容
html:
<div>你选中我之后,弹出我!逗你玩</div>
js1:
function test(){
var txt = window.getSelection?window.getSelection():document.selection.createRange().text;
alert(txt)
}
document.onmouseup = test
移除选中内容:
js2:
function test(){
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
}
document.onmouseup = test
window.getSelection和document.selection getSelection
原文:http://www.cnblogs.com/abby-WebDeveloper/p/6294077.html