jquery检测我们何时处于textarea中的文本末尾

我只是想学习jquery,我想知道你怎么能知道什么时候,在textarea中给出一堆文本,是textarea末尾的用户并试图点击。 例如,文本的最后一行是asdf,当用户在该行时,按下箭头键,我想得到某种事件,或者至少能够弄清楚我们是否在最后一行,并做一个动作(例如,环绕并转到第一行)

如果有人知道如何做到这一点,将不胜感激!

贾森

如果您在用户填写时尝试调整textarea的大小,则已经有一个Jquery插件:

plugins.jquery.com/project/TextAreaResizer

查看http://jacklmoore.com/autosize/

它很简单:

// Example: $(document).ready(function(){ $('textarea').autosize(); }); 

如果您希望textarea在用户填写时扩展,您可以执行以下操作:

 function resizeTextarea(textarea) { var defaultHeight = 150; var lines = textarea.val().split("\n"); var count = lines.length; $.each(lines, function() { count += parseInt(this.length / 70); }); var currHeight = textarea.height(); var rows = parseInt(currHeight / 20) - 1; if (count > rows && currHeight < 600) { // Increase height unless 600 or higher textarea.css("height", (currHeight + (defaultHeight/2))); } else if (((count + 7) <= rows) && (currHeight > defaultHeight) && (count > 5)) { // Decrease height textarea.css("height", (currHeight - (defaultHeight/2))); } else if ((count <= rows) && (count <= 5)) { // If less than 5 rows, use default height textarea.css("height", defaultHeight); } } $('textarea.resizable').each(function() { $(this).bind("keypress input beforepaste", function(){ resizeTextarea(event.target); }); resizeTextarea($(this)); // Initial refresh });