现在的位置: 首页 > 综合 > 正文

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

2019年11月17日 ⁄ 综合 ⁄ 共 3481字 ⁄ 字号 评论关闭

<!
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 runat
=
"
server
"
>


    

<
title
>
无标题页
</
title
>


    

<
script type 
=
 
"
text/javascript
"
 src 
=
 
"
JScript.js
"
></
script
>
 

    

    

<
script type
=
"
text/javascript
"
>
  


    window.onload 

=
 function()

    


{

        var ci 

=
 
new
 CursorInsert();

        ci.SetTargetTextControl(document.getElementById(

'
txtContent
'
));

        ci.SetSourceControl(document.getElementById(

'
btnSource
'
), 
'
[note]NOTE[/note]
'
);

        

        ci.Initialize();

    }





    

</
script
>
   

</
head
>


<
body
>


    

<
form id
=
"
form1
"
 runat
=
"
server
"
>


    

<
textarea id 
=
 
"
txtContent
"
 cols 
=
 
"
1
"
 rows 
=
 
"
1
"
 style 
=
 
"
width:500px;height:300px;
"
></
textarea
><
br 
/>


    

<
input id 
=
 
"
btnSource
"
 type 
=
 
"
button
"
 value 
=
 
"
Click me
"
 
/>


    

</
form
>


</
body
>


</
html
>

 

 

 

 

这是封装后的类:



var CursorInsert 

=
 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(

"
Please set target text control first!
"
);

            

return
 ;

        }




        

        

if
 (
this
.sourceControls.length 
==
 
0
)

        


{

            alert(

"
Please set source control first!
"
);

            

return
 ;

        }




        

        

this
.SetText(
this
.targetTextControl);

        

        

for
 (var i 
=
 
0
; i 
<
 
this
.sourceControls.length; i
++
)

        


{

            

this
.SetButton(
this
.sourceControls[i].Button,
this
.sourceControls[i].Value,
this
.targetTextControl);

        }




    }




    

    

this
.SetButton 
=
 function(btn,value,target)

    


{

        var ctrl 

=
 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)   

        


{  

            var caretPos 

=
 textObj.caretPos;  

            caretPos.text 

=
 caretPos.text.charAt(caretPos.text.length 
-
 
1

==
 
'
 
'
 
?
 value 
+
 
'
 
'
 : value;  

        }




        

else


        


{  

            textObj.value 

=
 value;  

        }


  

    }




    

else


    


{  

        

if
(textObj.setSelectionRange)

        


{  

            var rangeStart 

=
 textObj.selectionStart;  

            var rangeEnd 

=
 textObj.selectionEnd;  

            var tempStr1 

=
 textObj.value.substring(
0
,rangeStart);  

            var tempStr2 

=
 textObj.value.substring(rangeEnd);  

            textObj.value 

=
 tempStr1 
+
 value 
+
 tempStr2;  

        }




        

else


        


{  

            alert(

"
This version of Mozilla based browser does not support setSelectionRange!
"
);  

        }


  

    }


  

}

【上篇】
【下篇】

抱歉!评论已关闭.