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
Release: 2016-05-16 17:21:49
Original
1159 people have browsed it
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>



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template