Keep on going, never give up.

通过JavaScript实现TextArea光标定位

本文定位代码是早期收集于网络,无法溯源,非天缘原创,特别声明。主要实现功能是通过JAVASCRIPT实现对用户操作光标的定位,比如文本位置定位、文本选择等功能,代码也很精简,特别感谢原创者的用心。

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>光标位置</title> 
<style> 
</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="200">boundingHeight:&nbsp;<span id="bh"></span></td> </tr> 
<tr> <td width="200">boundingWidth:&nbsp;<span id="bw"></span></td> </tr> 
<tr> <td width="200">boundingTop:&nbsp;<span id="bt"></span></td> </tr> 
<tr> <td width="200">boundingLeft:&nbsp;<span id="bl"></span></td> </tr> 
<tr> <td width="200">offsetLeft:&nbsp;<span id="ol"></span> </td> </tr> 
<tr> <td width="200">offsetTop:&nbsp;<span id="ot"></span> </td> </tr> 
<tr> <td width="200">text:&nbsp;<span style="position: absolute; z-index: 10" id="t"></span> </td> </tr> 
</table> 
</body> 
</html> 

 

相关评论(0):  

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

订阅博客

最新文章

本站采用创作共用版权协议, 要求署名、非商业用途和保持一致. 转载也必须遵循“署名-非商业用途-保持一致”的创作共用协议. 返回顶部
Copyright@2005-2016 Metsky.com, All rights Reserved.