Home >Web Front-end >JS Tutorial >js code to insert value at cursor position_javascript skills

js code to insert value at cursor position_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:21:491157browse
Copy code The code is as follows:

 /** <br>*Insert a string into the input field (where the cursor is) <br>*@param $t document.getElementById('fieldId') <br>*@param myValue The value to be inserted<br>* * <br>function addSplitToField($t,myValue){ <br>if (document.selection) { <br>$t.focus(); <br>sel = document.selection.createRange(); <br>sel .text = myValue; <br>$t.focus(); <br>}else if($t.selectionStart || $t.selectionStart == '0') { <br>var startPos = $t.selectionStart; <br>var endPos = $t.selectionEnd; <br>var scrollTop = $t.scrollTop; <br>$t.value = $t.value.substring(0, startPos) myValue $t.value.substring(endPos , $t.value.length); <br>this.focus(); <br>$t.selectionStart = startPos myValue.length; <br>$t.selectionEnd = startPos myValue.length; <br>$t. scrollTop = scrollTop; <br>}else{ <br>$t.value = myValue; <br>$t.focus(); <br>} <br>} <br>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn