<script language="javascript">
var agt=navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
function a(){
var myArea = document.getElementById("s");
var selection;
if (!ie){
if (myArea.selectionStart!= undefined) {
selection = myArea.value.substr(myArea.selectionStart, myArea.selectionEnd - myArea.selectionStart);
}
}else{
if (window.getSelection){
selection = window.getSelection();
}else if (document.getSelection){
selection = document.getSelection();
}else if (document.selection){
selection = document.selection.createRange().text;
}
}
if(selection!='')alert(selection)
}
</script>
<textarea type="text" value="testtest" id="s" name="s"
οnmοuseup="javascript:a()" ></textarea>
一段兼容各浏览器的获取文本框或文本域选中内容的代码
最新推荐文章于 2021-06-29 11:14:04 发布