在文本框的光标处插入指定的文本(兼容IE6和Firefox)

<! DOCTYPEhtmlPUBLIC " -//W3C//DTDXHTML1.0Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< htmlxmlns = " http://www.w3.org/1999/xhtml " >
< headrunat = " server " >
< title > 无标题页 </ title >
< scripttype = " text/javascript " src = " JScript.js " ></ script >

< scripttype = " text/javascript " >

window.onload
= function()
{
varci
= new CursorInsert();
ci.SetTargetTextControl(document.getElementById(
' txtContent ' ));
ci.SetSourceControl(document.getElementById(
' btnSource ' ), ' [note]NOTE[/note] ' );

ci.Initialize();
}


</ 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()
{
this .targetTextControl = null ;
this .sourceControls = new Array();

this .SetTargetTextControl = function(ctrl)
{
this .targetTextControl = ctrl;
}


this .SetSourceControl = function(ctrl,value)
{
this .sourceControls[ this .sourceControls.length] = { " Button " :ctrl, " Value " :value} ;
}


this .Initialize = function()
{
if ( this .targetTextControl == null )
{
alert(
" Pleasesettargettextcontrolfirst! " );
return ;
}


if ( this .sourceControls.length == 0 )
{
alert(
" Pleasesetsourcecontrolfirst! " );
return ;
}


this .SetText( this .targetTextControl);

for (vari = 0 ;i < this .sourceControls.length;i ++ )
{
this .SetButton( this .sourceControls[i].Button, this .sourceControls[i].Value, this .targetTextControl);
}

}


this .SetButton = function(btn,value,target)
{
varctrl
= target == null ? this .TextControl:target;

btn.onclick
= function()
{
CursorInsert.prototype.InsertAtCaret(target,value);
}

}


this .SetText = function(ctrl)
{
ctrl.onselect
= function()
{
CursorInsert.prototype.SetCaret(
this );
}


ctrl.onclick
= function()
{
CursorInsert.prototype.SetCaret(
this );
}


ctrl.onkeyup
= function()
{
CursorInsert.prototype.SetCaret(
this );
}

}

}


CursorInsert.prototype.SetCaret
= function(textObj)
{
if (textObj.createTextRange)
{
textObj.caretPos
= document.selection.createRange().duplicate();
}

}


CursorInsert.prototype.InsertAtCaret
= function(textObj,value)
{
if (document.all)
{
if (textObj.createTextRange && textObj.caretPos)
{
varcaretPos
= textObj.caretPos;
caretPos.text
= caretPos.text.charAt(caretPos.text.length - 1 ) == ' ' ? value + ' ' :value;
}

else
{
textObj.value
= value;
}

}

else
{
if (textObj.setSelectionRange)
{
varrangeStart
= textObj.selectionStart;
varrangeEnd
= textObj.selectionEnd;
vartempStr1
= textObj.value.substring( 0 ,rangeStart);
vartempStr2
= textObj.value.substring(rangeEnd);
textObj.value
= tempStr1 + value + tempStr2;
}

else
{
alert(
" ThisversionofMozillabasedbrowserdoesnotsupportsetSelectionRange! " );
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值