<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta name="generator" content="microsoft frontpage 4.0">
<meta name="progid" content="frontpage.editor.document">
<title>光标位置</title>
<style>
input{border: 1 solid #000000}
body,table{font-size: 10pt}
</style>
</head>
<body>
<table border="0" width="700" cellspacing="0" cellpadding="0">
<tr>
<td width="479" rowspan="7">
点击 textarea 实现光标定位
<p>
<textarea rows="7" cols="49" id="box" onclick=tellpoint()>我怕来不及我要抱着你,直到感觉你的绉纹有了岁月的痕迹,直到视线变得模糊直到不能呼吸
为了你我愿意
动也不能动也要看着你,直到感觉你的发线有了白雪的痕迹,直到肯定你是真的直到失去力气让我们形影不离
如果全世界我也可以放弃,至少还有你值得我去珍惜而你在这里就是生命的奇迹
也许全世界我也可以忘记,就是不愿意失去你的消息你掌心的痣我总记得在哪里
我们好不容易我们身不由已,我怕时间太快不够将你看仔细,我怕时间太慢日夜担心失去你恨不得一夜之间白头永不分离
</textarea>
<script>
function movepoint()
{
var pn = parseint(pnum.value);
if(isnan(pn))
return;
var rng = box.createtextrange();
rng.movestart("character",pn);
rng.collapse(true);
rng.select();
returncase(rng)
}
function tellpoint()
{
var rng = event.srcelement.createtextrange();
rng.movetopoint(event.x,event.y);
rng.movestart("character",-event.srcelement.value.length)
pnum.value = rng.text.length
returncase(rng)
}
function returncase(rng)
{
bh.innertext = rng.boundingheight;
bl.innertext = rng.boundingleft;
bt.innertext = rng.boundingtop;
bw.innertext = rng.boundingwidth;
ot.innertext = rng.offsettop;
ol.innertext = rng.offsetleft;
t.innertext = rng.text;
}
function selecttext(sp,ep)
{
sp = parseint(sp)
ep = parseint(ep)
if(isnan(sp)||isnan(ep))
return;
var rng = box.createtextrange();
rng.moveend("character",-box.value.length)
rng.movestart("character",-box.value.length)
rng.collapse(true);
rng.moveend("character",ep)
rng.movestart("character",sp)
rng.select();
returncase(rng);
}
var rg = box.createtextrange();
function findtext(tw)
{
if(tw=="")
return;
var sw = 0;
if(document.selection)
{
sw = document.selection.createrange().text.length;
}
rg.moveend("character",box.value.length);
rg.movestart("character",sw);
if(rg.findtext(tw))
{
rg.select();
returncase(rg);
}
if(rg.text!=tw)
{
alert("已经搜索完了")
rg = box.createtextrange()
}
}
</script>
</p>
<p></p>
光标位置:<input type="text" value="0" id="pnum" size="8"> <input type="button" onclick="movepoint()" value="移动光标到指定位置">
<p></p>
选择指定范围:<input type="text" size="9" id="sbox"> — <input type="text" size="9" id="ebox"> <input type="button" onclick="selecttext(sbox.value,ebox.value)" value="选择">
<p></p>
选择查找字符 :<input type="text" value="" id="cbox" size="8"> <input type="button" onclick="findtext(cbox.value)" value="查找下一个并选择">
</td>
<td width="217">boundingheight: <span id="bh"></span></td>
</tr>
<tr>
<td width="217">boundingwidth: <span id="bw"></span></td>
</tr>
<tr>
<td width="217">boundingtop: <span id="bt"></span></td>
</tr>
<tr>
<td width="217">boundingleft: <span id="bl"></span></td>
</tr>
<tr>
<td width="217">offsetleft: <span id="ol"></span> </td>
</tr>
<tr>
<td width="217">offsettop: <span id="ot"></span> </td>
</tr>
<tr>
<td width="217">text: <span style="position: absolute; z-index: 10" id="t"></span> </td>
</tr>
</table>
</body>
</html>