//脚本获取网页中选中文字
var word = document.selection.createRange().text;
//获取选中文字所在的句子
var range = document.selection.createRange();
range.expand("sentence");
var sentence = range.text;
alert(word);//弹出选中的文字
alert(sentence );
<input type="text" id="txt" value="abcdefghijklmn"/>
var o = document.getElementById("txt");
o.setSelectionRange(len-3,len);
//获取光标起始位置
if (textarea.setSelectionRange) {
pos = textarea.selectionStart;
}
function locatePoint(){
var aCtrl = document.getElementById("txtContent");
if (aCtrl.setSelectionRange) {
setTimeout(function() {
aCtrl.setSelectionRange(0, 0); //将光标定位在textarea的开头,需要定位到其他位置的请自行修改
aCtrl.focus();
}, 0);
}else if (aCtrl.createTextRange) {
var textArea=document.getElementById("txtContent");
var tempText=textArea.createTextRange();
tempText.moveEnd("character",0-tempText.text.length);
tempText.select();
}
}