ITeye博客

js设置光标选中text中指定文字

xiaodi667 · 2012-12-13 13:11 · js · Web前端 · Markdown


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<textarea id="o"></textarea>
<input type="button" value="aaaa" onclick="setSelectText(document.getElementById('o'),3,5)" />
<script>
function setSelectText(el,start,end){
	if(el.createTextRange){
		var Range=el.createTextRange();
		Range.collapse();
		Range.moveEnd('character',end);
		Range.moveStart('character',start);
		Range.select();
	}
if(el.setSelectionRange){
        el.focus();  
        el.setSelectionRange(start,end);  //设光标 
    }
}
</script>
</body>
</html>


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。