jquery笔墨插入光标插件,兼容FF,IE6+,Chrom
发布时间:2018-08-29 14:05:20 所属栏目:业界 来源:站长网
导读:这是一款jquery的插件,首要用途是将一段文本信息插入到光标处,这个成果听起来很是简朴,但现实做出来可没那么轻易,首要是由于兼容性的题目,其后查阅很多资料,才发明白这款短小精壮的jquery插件,和各人分享下吧. (function($){ $.fn.extend({ insertAtCaret:
这是一款jquery的插件,首要用途是将一段文本信息插入到光标处,这个成果听起来很是简朴,但现实做出来可没那么轻易,首要是由于兼容性的题目,其后查阅很多资料,才发明白这款短小精壮的jquery插件,和各人分享下吧. (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(“text”); 假如你不风俗这样的方法,你可以将它改为正常的函数,譬喻: function insertAtCaret(obj,myValue){ var $t=obj[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(); } } 用法和上面的相同,insertAtCaret传入一个jquery工具,以及所要插入的文本,insertAtCaret($(“select”),”text”); (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐
热点阅读