标签:
$.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(); } } });
<textarea id="content"></textarea> <a href="#" onclick="insertExp()">插入文本</a> <script type="text/javascript"> function insertExp() { var exp = $("#expSelect").val(); $("#content").insertAtCursor("测试插入文本"); } </script>
标签:
原文地址:http://my.oschina.net/javawdw/blog/517500