通过 javascript 在光标处插入文本

功能:实现将指定文本插入到目标文本框中的最后一次光标位置

通过一个窗口的全局变量记录目标文本框的最后一次光标位置,因为当你离开此文本框之后,焦点也就移开了。

获取光标位置(包括选中文本时的起始位置)使用函数,采用 Marshall 的方法:
http://blog.csdn.net/liujin4049/archive/2006/09/19/1244065.aspx
非常感谢 Marshall emthup.gif

None.gif <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
None.gif
<!-- http://community.csdn.net/Expert/TopicView3.asp?id=5649731 -->
None.gif
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head >
None.gif    
< title > Insert Text into TextBox at Cursor Position by Javascript </ title >
ExpandedBlockStart.gifContractedBlock.gif    
< script  type ="text/javascript" > dot.gif
InBlock.gif    
var cursPos; // 窗口全局变量,保存目标 TextBox 的最后一次活动光标位置
ExpandedSubBlockStart.gifContractedSubBlock.gif
    function insertText() dot.gif{    
InBlock.gif        
var txt1 = document.getElementById("Text1");
InBlock.gif        
var txt2 = document.getElementById("Text2");        
InBlock.gif        
//debugger;
InBlock.gif
        if(!cursPos) TraceCursorPosition(txt2); // 获取光标位置        
InBlock.gif
        txt2.value = txt2.value.slice(0, cursPos.start) + 
InBlock.gif            txt1.value 
+ txt2.value.slice(cursPos.end)
ExpandedSubBlockEnd.gif    }
    
InBlock.gif
InBlock.gif    
// 跟踪光标位置
InBlock.gif
    function TraceCursorPosition(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        
//debugger;
InBlock.gif
        cursPos = $CursorPosition(obj);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
// 返回光标所在位置
ExpandedSubBlockStart.gifContractedSubBlock.gif
    /**//*
InBlock.gif     * source: http://blog.csdn.net/liujin4049/archive/2006/09/19/1244065.aspx
InBlock.gif     * acknowledges for Marshall 
InBlock.gif     * example: 
InBlock.gif     *  var myTextBox = document.getElementById("MyTextBoxID");
InBlock.gif     *  var cursPos = $CursorPosition(myTextBox);
InBlock.gif     *  alert(cursPos.item[0] + " " + cursPos.item[1]);
InBlock.gif     *  // OR
InBlock.gif     *  alert(cursPos.start + " " + cursPos.end);    
ExpandedSubBlockEnd.gif    
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
function $CursorPosition(textBox)dot.gif{        
InBlock.gif        
var start = 0, end = 0;
InBlock.gif        
//如果是Firefox(1.5)的话,方法很简单
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if(typeof(textBox.selectionStart) == "number")dot.gif{
InBlock.gif            start 
= textBox.selectionStart;
InBlock.gif            end 
= textBox.selectionEnd;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//下面是IE(6.0)的方法,麻烦得很,还要计算上'\n'
ExpandedSubBlockStart.gifContractedSubBlock.gif
        else if(document.selection) dot.gif{
InBlock.gif            
var range = document.selection.createRange();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(range.parentElement().id == textBox.id) dot.gif{
InBlock.gif                
// create a selection of the whole textarea
InBlock.gif
                var range_all = document.body.createTextRange();
InBlock.gif                range_all.moveToElementText(textBox);
InBlock.gif                
//两个range,一个是已经选择的text(range),一个是整个textarea(range_all)
InBlock.gif
                //range_all.compareEndPoints()比较两个端点,如果range_all比range更往左(further to the left),则                
InBlock.gif
                //返回小于0的值,则range_all往右移一点,直到两个range的start相同。
InBlock.gif
                // calculate selection start point by moving beginning of range_all to beginning of range
InBlock.gif
                for (start=0; range_all.compareEndPoints("StartToStart", range) < 0; start++)
InBlock.gif                    range_all.moveStart('character', 
1);
InBlock.gif                
// get number of line breaks from textarea start to selection start and add them to start
InBlock.gif
                // 计算一下\n
ExpandedSubBlockStart.gifContractedSubBlock.gif
                for (var i = 0; i <= start; i ++dot.gif{
InBlock.gif                    
if (textBox.value.charAt(i) == '\n')
InBlock.gif                        start
++;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// create a selection of the whole textarea
InBlock.gif
                var range_all = document.body.createTextRange();
InBlock.gif                range_all.moveToElementText(textBox);
InBlock.gif                
// calculate selection end point by moving beginning of range_all to end of range
ExpandedSubBlockStart.gifContractedSubBlock.gif
                for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end ++dot.gif{
InBlock.gif                    range_all.moveStart('character', 
1);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
// get number of line breaks from textarea start to selection end and add them to end
ExpandedSubBlockStart.gifContractedSubBlock.gif
                for (var i = 0; i <= end; i ++dot.gif{
InBlock.gif                    
if (textBox.value.charAt(i) == '\n')
InBlock.gif                        end 
++;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
//return [start, end]; // 包括选中区域的起始位置
InBlock.gif
        // modified to return as Object
ExpandedSubBlockStart.gifContractedSubBlock.gif
        return dot.gif{"start": start, "end": end, "item": [start, end]};
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    
</ script >
None.gif
</ head >
None.gif
< body >
None.gif    input :
< input  id ="Text1"  type ="text"   />< input  id ="Button1"  type ="button"  value ="Insert"  onclick ="insertText()"   />< br  />
None.gif    output:
< textarea  id ="Text2"  cols ="40"  rows ="10"  onkeydown ="TraceCursorPosition(this)"  onkeypress ="TraceCursorPosition(this)"  onfocus ="TraceCursorPosition(this)"  onselect ="TraceCursorPosition(this)"  onmouseover ="TraceCursorPosition(this)"  onmousedown ="TraceCursorPosition(this)" > hello world! </ textarea >< br  />
None.gif
None.gif
</ body >
None.gif
</ html >
None.gif

下载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值