控制选择位置

function Select(posStart, posEnd) {
	var posStart = parseInt(posStart) || 0;
	var posEnd = parseInt(posEnd) || 0;
	var input = document.getElementById("myText");
	if(posStart > posEnd){  //在Chrome中试了,End要比Start大
		var tmp = posStart;
		posStart = posEnd;
		posEnd = tmp;
	}
	if ('selectionStart' in input) {
		input.selectionStart = posStart;//选择的开始位置 0 为最左
		input.selectionEnd = posEnd;  //选择结束的位置
		input.focus();
	}
	else { // Internet Explorer before version 9
		var inputRange = input.createTextRange();
		inputRange.moveStart("character", posStart);//选择开始
		inputRange.collapse();
		inputRange.moveEnd("character", posEnd);  //选择结束
		inputRange.select();
	}
}

  

posted @ 2012-02-08 16:04  Arliang  阅读(231)  评论(1编辑  收藏  举报