/** * tId 文本域Id * tag 插入内容 */ ffunction addEmoticon(tId,tag) { var txtarea = document.getElementById(tId); //IE if(document.selection) { var theSelection = document.selection.createRange().text; if(!theSelection) { theSelection=tag} txtarea.focus(); if(theSelection.charAt(theSelection.length - 1) == " "){ theSelection = theSelection.substring(0, theSelection.length - 1); document.selection.createRange().text = theSelection+ " "; } else { document.selection.createRange().text = theSelection; } } // Mozilla else if(txtarea.selectionStart || txtarea.selectionStart == '0'){ var startPos = txtarea.selectionStart; var endPos = txtarea.selectionEnd; var myText = (txtarea.value).substring(startPos, endPos); if(!myText) { myText=tag;} if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any subst = myText.substring(0, (myText.length - 1))+ " "; } else { subst = myText; } txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length); txtarea.focus(); var cPos=startPos+(myText.length); txtarea.selectionStart=cPos; txtarea.selectionEnd=cPos; } // All others else{ txtarea.value+=tag; txtarea.focus(); } if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate(); }
jquery写法(主要部分):
(function($) { $.fn.insertAtCaret = function (tagName) { return this.each(function(){ if (document.selection) { //IE support var theSelection = document.selection.createRange().text; if(!theSelection) { theSelection=tagName} this.focus(); if(theSelection.charAt(theSelection.length - 1) == " "){ theSelection = theSelection.substring(0, theSelection.length - 1); document.selection.createRange().text = theSelection+ " "; } else { document.selection.createRange().text = theSelection; } }else if (this.selectionStart || this.selectionStart == '0') { //MOZILLA/NETSCAPE support startPos = this.selectionStart; endPos = this.selectionEnd; scrollTop = this.scrollTop; this.value = this.value.substring(0, startPos) + tagName + this.value.substring(endPos,this.value.length); this.focus(); this.selectionStart = startPos + tagName.length; this.selectionEnd = startPos + tagName.length; this.scrollTop = scrollTop; } else { this.value += tagName; this.focus(); } if(this.createTextRange) this.caretPos = document.selection.createRange().duplicate(); }); }; })(jQuery);
转:http://www.zhudongdong.cn/html/347.html
相关推荐
在textarea光标处插入文本
使用JS在textarea在光标处插入内容.txt
NULL 博文链接:https://cai555.iteye.com/blog/618161
本文实例讲述了jQuery往textarea中光标所在... /* 在textarea处插入文本--Start */ (function($) { $.fn.extend({ insertContent : function(myValue, t) { var $t = $(this)[0]; if (document.selection) { //
textarea光标定位在最后,就是往textarea中赋值时候,光标自动移到最后。
本实例使用Javascript实现在textarea光标处插入文本,支持多种浏览器
JS在TextArea光标位置插入文字+移动光标到文字末尾,Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd属性,具体实现如下,感兴趣的朋友可以参考下哈
textarea 自适应大小 兼容Chrome ie等浏览器,自动调整大小
获取div编辑框,textarea,input text的光标位置 兼容IE,FF和Chrome的方法介绍,有需求的朋友可以参考
于是搜索了baidu,google,找到了 IE下面的方法 代码如下: var tea=document.getElementById(“文本框的ID”); var txt=textArea.createTextRange(); txt.moveEnd(“character”,0-tempText.text.lengt
JavaScript获取,设置光标位置,兼容InputTextArea
vue通过当前的光标来进行插值,从而需要去获取光标所在的位置然后进行对光标前后的字符串进行截取处理,从而达到对新字符的插入
这种主要在手机端应用较为广泛,当前PC也是可以用的了。 使用简单,也可以通过JS触发事件调整大小
jQuery扩展函数,用于获取设置textarea中光标位置
在DOM里面操作textarea里面的字符,是比较麻烦的。于是我有这个封装分享给大家,测试过IE6,8, firefox ,chrome, opera , safari。兼容没问题。
jquery textarea插入gif动态QQ表情图片表单提交 jquery textarea插入gif动态QQ表情图片表单提交
jQuery textArea文本光标全功能插件,可以获取光标位置,向光标文字插入文本,高亮显示指定的文本块