实用jquery扩展收集(1):在光标处插入内容 和 获取textarea选中的值
2013-05-22 13:57
459 查看
(1):在光标处插入值
(2):获取textarea选中的值
$.fn.extend({ insertAtCursor : 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(); } } });
(2):获取textarea选中的值
$.fn.selection = function(){ var s,e,range,stored_range; if(this[0].selectionStart == undefined){ var selection=document.selection; if (this[0].tagName.toLowerCase() != "textarea") { var val = this.val(); range = selection.createRange().duplicate(); range.moveEnd("character", val.length); s = (range.text == "" ? val.length:val.lastIndexOf(range.text)); range = selection.createRange().duplicate(); range.moveStart("character", -val.length); e = range.text.length; }else { range = selection.createRange(), stored_range = range.duplicate(); stored_range.moveToElementText(this[0]); stored_range.setEndPoint('EndToEnd', range); s = stored_range.text.length - range.text.length; e = s + range.text.length; } }else{ s=this[0].selectionStart, e=this[0].selectionEnd; } var te=this[0].value.substring(s,e); return {start:s,end:e,text:te}; };
相关文章推荐
- 实用jquery扩展收集(1):在光标处插入内容 和 获取textarea选中的值
- JS获取textarea的光标位置并插入内容(转)
- 【jQuery插件】insertContent-在文本框光标位置插入内容并选中
- 基于jQuery的输入框在光标位置插入内容, 并选中
- jQuery 输入框 在光标位置插入内容, 并选中
- javascript 获取 textarea 当前光标 并插入内容
- JQuery扩展:TextArea在光标处插入文本
- jQuery 输入框 在光标位置插入内容/选中
- javascript 获取 textarea 当前光标 并插入内容
- 【jQuery插件】insertContent-在文本框光标位置插入内容并选中
- 自定义insertContent方法的JQuery插件实现在文本框光标位置插入内容并选中
- javascript获取textarea光标选择位置和内容方法(IE, Firefox)
- JQuery在光标位置插入内容的实现代码
- javascript获取textarea光标选择位置和内容方法(IE, Firefox)
- jQuery插件,将内容插入到光标处
- Jquery在光标位置插入内容
- jquery中focus()函数实现当对象获得焦点后自动把光标移到内容最后 //获取焦点后光标在字符串后 //其原理就是获得焦点后重新把自己复制粘帖一下 var t=$("#"+id).val()
- javascript获取textarea光标选择位置和内容方法(IE, Firefox)
- asp.net使用jQuery获取RadioButtonList成员选中内容和值示例