网页有一个div,里面有些文字,鼠标选中其中一部分字,怎么能感知这一事件,然后返回选中内容。
我在贴吧也看到类似的问题,是同一个吗?
<!--支持IE9+ Firefox Chrome-->
<p>这是一段测试文字</p>
<script type='text/javascript'>
function getSlct(){//获取选中文字
return getSelection ? getSelection() : document.selection.createRange();
}
function cancelSlct(){//清除选中效果
return getSelection ? getSelection().removeAllRanges() : document.selection.empty();
}
document.onmouseup=function(e){
var e = e || event,
slcted = getSlct();
if(slcted != '')alert(slcted);
cancelSlct();
}
</script>