往textarea中光标所在位置插入文本
2015-09-06 17:00
344 查看
来自于http://www.oschina.net/code/snippet_1780998_36843
$(".addFullIn").click( function () { $(".questionName").insertContent("( )"); }); //$(".addFullIn")为点击按钮 //$(".questionName")为要插入的文本框 //insertContent("( )")括号内为要插入的内容 (function ($) { $.fn.extend({ insertContent: function (myValue, t) { var $t = $(this)[0]; if (document.selection) { // ie this.focus(); var sel = document.selection.createRange(); sel.text = myValue; this.focus(); sel.moveStart('character', -l); var wee = sel.text.length; if (arguments.length == 2) { var l = $t.value.length; sel.moveEnd("character", wee + t); t <= 0 ? sel.moveStart("character", wee - 2 * t - myValue.length) : sel.moveStart( "character", wee - t - myValue.length); sel.select(); } } 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; if (arguments.length == 2) { $t.setSelectionRange(startPos - t, $t.selectionEnd + t); this.focus(); } } else { this.value += myValue; this.focus(); } } }) })(jQuery);
相关文章推荐
- IOS第18天(2,CALayer自定义图层)
- 扣取头发
- 易元平台使用
- 机房收费系统 之 总体理解
- CentOS进入单用户模式
- 如何将一个HTML页面嵌套在另一个页面中
- 如何开启和关闭定时器
- UVA 211 The Domino Effect
- LRU CACHE
- 机房收费系统之常用语句
- Android Region代码分析
- Android第一步
- treap(树堆)
- 线性回归 Linear Regression
- Java多线程实现同时进行小球的自由落体与平抛
- [leetcode 274 275]H-Index I II
- [LeedCode OJ]#21 Merge Two Sorted Lists
- 水平方向的PickerVIew
- 对于war解压缩
- iCloud之创建一个数据库通过保存记录