input/textarea文本的选择与获取

//获取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();
}

你可能感兴趣的:(IE)