码迷,mamicode.com
首页 > Web开发 > 详细

jQuery插件,将内容插入到光标处

时间:2016-05-08 16:33:13      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

(function ($) {
	$.fn.extend({
		insertAtCaret : function (myValue) {
			var $t = $(this)[0];
			if (document.selection) {
				this.focus();
				sel = document.selection.createRange();
				sel.text = myValue;
				this.focus();
			} 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;
				} else {
					this.value += myValue;
					this.focus();
				}
		}
	})
})(jQuery);

//调用方法:
$("select").insertAtCaret();

jQuery插件,将内容插入到光标处

标签:

原文地址:http://www.cnblogs.com/linkhtml/p/5470739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!