- $(function() {
- $.fn.manhuaInsert = function(options) {
- var defaults = {
- Event : "click",
- isForm : true,
- id : "content",
- btn : "btn"
- };
- var options = $.extend(defaults,options);
- var $t = $(this);
- var $id = $("#"+options.id);
- var content = options.isForm ? $id.val() : $id.html();
- var len = content.length;
- var $btn = $("#"+options.btn);
- $btn.live(options.Event,function(e){
- if (document.selection) {
- $t.focus();
- var sel = document.selection.createRange();
- sel.text = content;
- $t.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 - len) : sel.moveStart("character", wee - t - len);
- 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) + content + $t.value.substring(endPos,$t.value.length);
- $t.focus();
- $t.selectionStart = startPos + len;
- $t.selectionEnd = startPos + len;
- $t.scrollTop = scrollTop;
- if (arguments.length == 2) {
- $t.setSelectionRange(startPos - t,$t.selectionEnd + t);
- $t.focus();
- }
- } else {
- this.value += content;
- $t.focus();
- }
- });
- };
- });
- //该片段来自于http://www.codesnippet.cn/detail/100720134539.html
来源: http://www.codesnippet.cn/detail/100720134539.html