jquery向指定光标位置插入内容
html
<input type="text" id="baseUrl" class="bp_input" /> <input type="button" class="var" value="(*)">
js
$.fn.extend({ insertAtCaret: function (myValue){var tttt=$(this)[0]; if (document.selection) { this.focus(); sel = document.selection.createRange(); sel.text = myValue; this.focus(); }else if (tttt.selectionStart || tttt.selectionStart == '0') { var startPos = tttt.selectionStart; var endPos = tttt.selectionEnd; var scrollTop = tttt.scrollTop; tttt.value = tttt.value.substring(0, startPos) + myValue + tttt.value.substring(endPos,tttt.value.length); this.focus(); tttt.selectionStart = startPos + myValue.length; tttt.selectionEnd = startPos + myValue.length; tttt.scrollTop = scrollTop; } else { this.value += myValue; this.focus(); } } }); $(".var").click(function () { $("#baseUrl").insertAtCaret($(this).val()); });