javascript下兼容firefox选取textarea文本的代码
人气:0
主要用到的js代码
function getSelectedText(){
var selectedText;
var textField=document.getElementById('inputTextarea');
if(window.getSelection) selectedText=getTextFieldSelection(textField);//getTextFieldSelection(document.getElementById("inputTextArea"));
else selectedText=document.selection.createRange().text;
alert(selectedText);
}
function getTextFieldSelection(e){
//var oEvent=arguments.callee.caller.arguments[0];
if(e.selectionStart != undefined && e.selectionEnd != undefined)
return e.value.substring(e.selectionStart,e.selectionEnd);
else return "";
}
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
复制代码 代码如下:
function getSelectedText(){
var selectedText;
var textField=document.getElementById('inputTextarea');
if(window.getSelection) selectedText=getTextFieldSelection(textField);//getTextFieldSelection(document.getElementById("inputTextArea"));
else selectedText=document.selection.createRange().text;
alert(selectedText);
}
function getTextFieldSelection(e){
//var oEvent=arguments.callee.caller.arguments[0];
if(e.selectionStart != undefined && e.selectionEnd != undefined)
return e.value.substring(e.selectionStart,e.selectionEnd);
else return "";
}
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
加载全部内容