日期:2014-05-17 浏览次数:20903 次
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script> <script type="text/javascript" src="Scripts/jquery-extend.js"></script> <script type="text/javascript"> $(document).ready(function () { $('textarea').keypress(function (e) { if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) { var text = $(this).val(); var index = $(this).position(); var text = text.substring(0, index) + "\r\n" + text.substr(index, text.length - index); $(this).val(text).position(index + 1); return false; } }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="test" runat="server" TextMode="MultiLine" Height="111px" Width="194px"></asp:TextBox> </div> </form> </body> </html>
// 由华宰编写,在此鸣谢 $.fn.extend({ position: function (value) { var elem = this[0]; if (elem && (elem.tagName == "TEXTAREA" || elem.type.toLowerCase() == "text")) { if ($.browser.msie) { var rng; if (elem.tagName == "TEXTAREA") { rng = event.srcElement.createTextRange(); rng.moveToPoint(event.x, event.y); } else { rng = document.selection.createRange(); } if (value === undefined) { rng.moveStart("character", -event.srcElement.value.length); return rng.text.length; } else if (typeof value === "number") { var index = this.position(); index > value ? (rng.moveEnd("character", value - index)) : (rng.moveStart("character", value - index)) rng.select(); } } else { if (value === undefined) { return elem.selectionStart; } else if (typeof value === "number") { elem.selectionEnd = value; elem.selectionStart = value; } } } else { if (value === undefined) return undefined; } } }) $.fn.selectRange = function (start, end) { return this.each(function () { if (this.setSelectionRange) { this.focus(); this.setSelectionRange(start, end); } else if (this.createTextRange) { var range = this.createTextRange(); range.collapse(true); range.moveEnd('character', end); range.moveStart('character', start); range.select(); } }); };
参考内容: