HTMLInputElement.setSelectionRange()

HTMLInputElement.setSelectionRange()方法用来设置元素当前选中的文本的开始和结束位置。

其接口定义如下:

inputElement.setSelectionRange(
selectionStart
,selectionEnd
,[optional] selectionDirection
);

其中:

  • selectionStart:必选参数,选中的字符(串)在文本中的起始位置(包括该字符),number,大于等于0,小于等于selectionEnd
  • selectionEnd:必选参数,选中的字符(串)在文本中的结束位置(不包括该字符),number,大于等于selectionStart
  • selectionDirection:可选参数,标识是从前向后还是从后向前选中的,string,取值backwardforwardnone。(较新的浏览器支持,暂时没发现有什么用处)

注意:

  • 要想看到选中效果,必须要使获得焦点。
  • 如果selectionStart等于selectionEnd,则不出现字符(串)选中效果,但可使光标定位到selectionStart之前。

你可能感兴趣的:(HTMLInputElement.setSelectionRange())