input/textarea text of choice and access

获得input/textarea的选择文本

 

  
  
  
  
  1. function getSelectedText(textbox){  
  2.     if(document.selection){//IE  
  3.        return document.selection.createRange().text;  
  4.     }else{  
  5.        return textbox.value.substring(textbox.selectionStart,textbox.selectionEnd);  
  6.      }  
  7. }  

设置 选择input/textarea里的文本

 

  
  
  
  
  1. function selectText(textbox,startIndex,stopIndex){  
  2.     if(textbox.setSelectionRange){  
  3.         textbox.setSelectionRange(startIndex,stopIndex);  
  4.      }  
  5.     else if(textbox.createTextRange){//IE  
  6.        var range = textbox.createTextRange();  
  7.        range.collapse(true);  
  8.        range.moveStart('character',startIndex);  
  9.        range.moveEnd('character',stopIndex-startIndex);  
  10.        range.select();  
  11.      }  
  12.     textbox.focus();  

 

你可能感兴趣的:(JavaScript,input,select,textarea,range)