Skip to content

Instantly share code, notes, and snippets.

@xyqfer
Created September 28, 2013 07:21
Show Gist options
  • Select an option

  • Save xyqfer/6739475 to your computer and use it in GitHub Desktop.

Select an option

Save xyqfer/6739475 to your computer and use it in GitHub Desktop.
文本框中选择指定长度的文本
function selectText(textbox, startIndex, stopIndex) {
if (textbox.setSelectionRange) {
textbox.setSelectionRange(startIndex, stopIndex);
} else if (textbox.createTextRange) {
var range = textbox.createTextRange();
range.collapse(true);
range.moveStart("character", startIndex);
range.moveEnd("character", stopIndex - startIndex);
range.select();
}
textbox.focus();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment