当前位置: 代码迷 >> Web前端 >> input/textarea文本的抉择与获取
  详细解决方案

input/textarea文本的抉择与获取

热度:204   发布时间:2012-11-23 22:54:33.0
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();
}
1 楼 Vimesly 2010-08-31  
 
  相关解决方案