您的位置:首页 > 其它

文本框中的字符串截取方法

2011-05-15 15:09 134 查看
function getSelectedText(textbox) {
if (document.selection) {
return document.selection.createRange().text;
} else {
return textbox.value.substring(textbox.selectionStart,textbox.selectionEnd);
}
}


IE9之前不提供selectionStart,selectionEnd属性

下面是根据起始位置和结束位置选取字符串:

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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: