日期:2014-05-16  浏览次数:20440 次

js获得光标所在的文本框(text/textarea)中的位置
<!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>
    <title>无标题页</title>

</head>
<body>
    <form name="form1" method="post"  id="form1">

        <a href="javascript:" onclick="SetContent(this.innerText)">用户名</a><br>
        <textarea name="txtTemp" rows="2" cols="20" id="txtTemp" onblur="onblurAction();" style="height:320px;width:700px;"></textarea>
                                
                                
    </form>
</body>
</html>
 
<script type="text/javascript"> 
 
function SetContent(text)
{
 txtTemp.focus();
 var sel = document.selection.createRange();
 sel.text=text;
}
   
function onblurAction() {
    txtTemp = event.srcElement;
}
 
</script>