获得input/textarea的选择文本
- function getSelectedText(textbox){
- if(document.selection){//IE
- return document.selection.createRange().text;
- }else{
- return textbox.value.substring(textbox.selectionStart,textbox.selectionEnd);
- }
- }
设置 选择input/textarea里的文本
- function selectText(textbox,startIndex,stopIndex){
- if(textbox.setSelectionRange){
- textbox.setSelectionRange(startIndex,stopIndex);
- }
- else if(textbox.createTextRange){//IE
- var range = textbox.createTextRange();
- range.collapse(true);
- range.moveStart('character',startIndex);
- range.moveEnd('character',stopIndex-startIndex);
- range.select();
- }
- textbox.focus();
- }