设置光标位置
源文件如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>设置光标</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<textarea id=demo cols=50 rows=5>设置光标位置</textarea>
<input type=checkbox id=collapse>倒数 第<input id=s value=4 size=4>位 <input value="设置光标" type=button οnclick=setCursor()>
<script>
function setCursor(){
var num=parseInt(document.all.s.value)
var range=document.all.demo.createTextRange();
if(document.all.collapse.checked){
range.collapse(false);
range.moveEnd('character',-1*num);
}else{
range.collapse(true);
range.moveStart('character',-1+num);
}
range.select();
}
</script>
</BODY>
</HTML>