javascript获取鼠标选中的文字(selection)在非IE浏览器
javascript获取鼠标选中的文字(selection)
selection是对当前激活选中区(即高亮文本)进行操作。
在非IE浏览器(Firefox、Safari、Chrome、Opera)下可以使用window.getSelection()获得selection对象,本文讲述的是标准的selection操作方法。
获取鼠标选中的文字的代码:
<textarea name="" id="test" cols="30" rows="10"></textarea>
<script type="text/javascript">
function getSelectText(){
var txt = null;
if (window.getSelection){ // mozilla FF
txt = window.getSelection();
}
else if (document.getSelection){
txt = document.getSelection();
}
else if (document.selection){ //IE
txt = document.selection.createRange().text;
}
return txt;
}
var obj=document.getElementById("test");
document.onmouseup=function(){
obj.value=getSelectText();
}
</script>
转自http://www.w3cking.com/javascript-selection/