Jquery inserts content at the cursor position. jquery inserts content at the cursor position.
This example describes how jquery inserts content at the cursor position. Share it with you for your reference. The specific implementation method is as follows:
Copy codeThe Code is as follows: <! DOCTYPE html>
<Html>
<Head>
<Title> No title page </title>
<Script type = "text/javascript" src = "jquery-1.7.2.min. Js "> </script>
<Script type = "text/javascript">
(Function ($ ){
$. Fn. extend ({
InsertAtCaret: function (myValue ){
Var $ t = $ (this) [0];
If (document. selection ){
This. focus ();
Sel = document. selection. createRange ();
Sel. text = myValue;
This. focus ();
} Else
If ($ t. selectionStart | $ t. selectionStart = '0 ')
{
Var startPos = $ t. selectionStart;
Var endPos = $ t. selectionEnd;
Var scrollTop = $ t. scrollTop;
$ T. value = $ t. value. substring (0, startPos) + myValue + $ t. value. substring (endPos, $ t. value. length );
This. focus ();
$ T. selectionStart = startPos + myValue. length;
$ T. selectionEnd = startPos + myValue. length;
$ T. scrollTop = scrollTop;
} Else {
This. value + = myValue;
This. focus ();
}
}
})
}) (JQuery );
$ (Document). ready (function (){
$ ("# Numd"). bind ("mouseleave", function (){
Document. getElementById ('keybored'). style. display = 'none ';
Document. getElementById ('nm '). blur ();
});
$ ("# Nm"). focus (function (){
Document. getElementById ('keybored'). style. display = '';
});
$ (". Readbtns"). click (function (){
$ ("# Nm"). insertAtCaret ($ (this). val ());
});
});
</Script>
</Head>
<Body>
<Ul>
<Li>
<Input/>
<Div>
</Div>
</Li>
</Ul>
<Input id = "hid" type = "text" value = "" style = "display: none"/>
<Span id = "numd" style = "border: 1px solid red; clear: both; display: inline-block; font: 800em;">
<Input type = "text" id = "Nm" name = "Nm" value = ""/>
<Div style = "display: none; border: 1px solid # A2B4C6; width: 150px; height: 400px ;"
Id = "keybored">
<Input type = "button" class = "readbtns" value = "1"/>
<Input type = "button" class = "readbtns" value = "2"/>
<Input type = "button" class = "readbtns" value = "3"/>
<Input type = "button" class = "readbtns" value = "4"/>
<Input type = "button" class = "readbtns" value = "5"/>
<Input type = "button" class = "readbtns" value = "6"/>
<Input type = "button" class = "readbtns" value = "7"/>
<Input type = "button" class = "readbtns" value = "8"/>
<Input type = "button" class = "readbtns" value = "9"/>
</Div>
</Span>
</Body>
</Html>
I hope this article will help you with jQuery programming.