<! DOCTYPEhtmlPUBLIC " -//W3C//DTDXHTML1.0Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
< htmlxmlns = " http://www.w3.org/1999/xhtml " >
< headrunat = " server " >
< title > 无标题页 </ title >
< scripttype = " text/javascript " src = " JScript.js " ></ script >
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
< scripttype = " text/javascript " >
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
window.onload = function()
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
varci = new CursorInsert();
ci.SetTargetTextControl(document.getElementById( ' txtContent ' ));
ci.SetSourceControl(document.getElementById( ' btnSource ' ), ' [note]NOTE[/note] ' );
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ci.Initialize();
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
</ script >
</ head >
< body >
< formid = " form1 " runat = " server " >
< textareaid = " txtContent " cols = " 1 " rows = " 1 " style = " width:500px;height:300px; " ></ textarea >< br />
< inputid = " btnSource " type = " button " value = " Clickme " />
</ form >
</ body >
</ html >
这是封装后的类:
varCursorInsert
=
function()
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
this .targetTextControl = null ;
this .sourceControls = new Array();
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
this .SetTargetTextControl = function(ctrl)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this .targetTextControl = ctrl;
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
this .SetSourceControl = function(ctrl,value)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
this .sourceControls[ this .sourceControls.length] =
{ " Button " :ctrl, " Value " :value} ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
this .Initialize = function()
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if ( this .targetTextControl == null )
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
alert( " Pleasesettargettextcontrolfirst! " );
return ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
if ( this .sourceControls.length == 0 )
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
alert( " Pleasesetsourcecontrolfirst! " );
return ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
this .SetText( this .targetTextControl);
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
for (vari = 0 ;i < this .sourceControls.length;i ++ )
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
this .SetButton( this .sourceControls[i].Button, this .sourceControls[i].Value, this .targetTextControl);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
this .SetButton = function(btn,value,target)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
varctrl = target == null ? this .TextControl:target;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
btn.onclick = function()
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
CursorInsert.prototype.InsertAtCaret(target,value);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
this .SetText = function(ctrl)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
ctrl.onselect = function()
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
CursorInsert.prototype.SetCaret( this );
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ctrl.onclick = function()
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
CursorInsert.prototype.SetCaret( this );
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
ctrl.onkeyup = function()
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
CursorInsert.prototype.SetCaret( this );
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
CursorInsert.prototype.SetCaret
=
function(textObj)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (textObj.createTextRange)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
textObj.caretPos = document.selection.createRange().duplicate();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
CursorInsert.prototype.InsertAtCaret
=
function(textObj,value)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
{
if (document.all)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if (textObj.createTextRange && textObj.caretPos)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
varcaretPos = textObj.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1 ) == ' ' ? value + ' ' :value;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
textObj.value = value;
}
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
if (textObj.setSelectionRange)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
varrangeStart = textObj.selectionStart;
varrangeEnd = textObj.selectionEnd;
vartempStr1 = textObj.value.substring( 0 ,rangeStart);
vartempStr2 = textObj.value.substring(rangeEnd);
textObj.value = tempStr1 + value + tempStr2;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
alert( " ThisversionofMozillabasedbrowserdoesnotsupportsetSelectionRange! " );
}
}
}