实例如下所示:
function insertatcursor(myfield, myvalue) { //ie 浏览器 if (document.selection) { myfield.focus(); sel = document.selection.createrange(); sel.text = myvalue; sel.select(); } //firefox、chrome等 else if (myfield.selectionstart || myfield.selectionstart == '0') { var startpos = myfield.selectionstart; var endpos = myfield.selectionend; // 保存滚动条 var restoretop = myfield.scrolltop; myfield.value = myfield.value.substring(0, startpos) + myvalue + myfield.value.substring(endpos, myfield.value.length); if (restoretop > 0) { myfield.scrolltop = restoretop; } myfield.focus(); myfield.selectionstart = startpos + myvalue.length; myfield.selectionend = startpos + myvalue.length; } else { myfield.value += myvalue; myfield.focus(); } } <textarea id="textarea" style="width: 386px; height: 260px"> </textarea> <input type="text" id="text" /> 相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
js的多线程运行库nexus.js使用详解
js做出移动端触摸轮播效果
js怎样将json格式数组下载到excel表格里
以上就是js操作txt文本在指定位置插入内容的详细内容。
