简体   繁体   English

自动Textarea光标移动

[英]Automatic Textarea cursor movement

My question is almost similar to Automatic newline in textarea in textarea but my context is more complex. 我的问题几乎类似于textarea中textarea中的自动换行,但我的上下文更复杂。 I have multiple textareas which have a 1 row attribute thus giving an impression of writing on air(just meaning empty area on the website with no borders since the textarea border i have set in css as >>> border: 0;) . 我有多个textareas有一行属性,因此给人一种空中写作的印象(只是意味着网站上没有边框的空白区域,因为textarea边界我在css中设置为>>> border:0;)

I would like to use JQuery or Javascript to calculate when the text entered by the user if he/she has reached the end of the row of the textarea so as to automatically move to the next textarea below. 我想使用JQuery或Javascript来计算用户输入的文本何时他/她已到达textarea行的末尾,以便自动移动到下面的下一个textarea。 One way to solve this would be to try and count the characters but different characters have different widths. 解决此问题的一种方法是尝试计算字符,但不同的字符具有不同的宽度。

I'm thinking of inserting a hidden element at the edge of the textareas to act as a trigger for the nexttextarea.focus() event but i have no idea of how i can achieve this. 我正在考虑在textareas的边缘插入一个隐藏元素作为nexttextarea.focus()事件的触发器,但我不知道如何实现这一点。

I have tried goofing around and thinking of different hacks but only one seems to be the solution... Try to store each character in an array and give them their default space taking value in px...like 'a'=>0.7px,'b'=>0.9px or something of the sort if their is a list somewhere (although it looks like it would take a lot of overhead in terms of memory as i would have to store both capital, small letters and many other characters.) Then do some calculations depending on the width of the browser if it has been re-sized or if not the full size to determine when the textarea row width becomes full at the browser width edge. 我已经尝试过四处寻找并考虑不同的黑客但只有一个似乎是解决方案...尝试将每个字符存储在一个数组中并给它们在px中取值的默认空间...就像'a'=> 0.7px ,'b'=> 0.9px或类似的东西,如果它们是某个地方的列表(虽然看起来它会占用很大的内存开销,因为我必须存储大写,小写字母和许多其他字符。)然后根据浏览器的宽度进行一些计算,如果它已被重新调整大小,或者如果不是全尺寸则确定textarea行宽在浏览器宽度边缘何时变满。 (For the time being the textarea width is 100% and has no parent therefore fills the whole browser width). (目前textarea宽度为100%且没有父级因此填充整个浏览器宽度)。

If anybody has an idea of a complex or simple way i can accomplish this, help me. 如果有人知道复杂或简单的方法,我可以做到这一点,帮助我。 A big problem is that IE and Mozilla introduce scroll-bars if the browser window is re-sized, and scroll-bars is what i never want the user to see(remember impression of typing into thin air). 一个很大的问题是,如果重新调整浏览器窗口大小,IE和Mozilla会引入滚动条,滚动条是我从不希望用户看到的内容(记住打字空气的印象)。

Forgive me for being so verbose..just wanted to be accurate and detailed. 原谅我是如此冗长......只是想要准确和详细。

This is harder than it looks. 这比它看起来更难。 Try checking the scrollHeight and scrollWidth of the textarea; 尝试检查textarea的scrollHeight和scrollWidth; if they changed, the text overflowed. 如果他们改变了,文字就会溢出来。 At that point, you can move text from the end of the current textarea to the beginning of the next textarea until the scroll height/width goes away. 此时,您可以将文本从当前textarea的末尾移动到下一个textarea的开头,直到滚动高度/宽度消失为止。

Here's an example: 这是一个例子:

document.onkeyup = function(evt) {
    var event = evt || window.event;
    var target = event.target;
    var nextArea = target.nextSibling; // assumes no whitespace between textareas.
    var chars;
    if (target.tagName == 'TEXTAREA' && (target.scrollLeft || target.scrollTop)) {
        chars = 0;
        while (target.scrollLeft || target.scrollTop) {
            target.value = target.value.replace(/.$/, function(m0) {
                nextArea.value = m0 + nextArea.value;
                return '';
            })
            ++chars;
            target.selectionStart = target.value.length;
        }
        nextArea.focus();
        nextArea.selectionStart = chars;
    }

}​

http://jsfiddle.net/L73RG/3/ http://jsfiddle.net/L73RG/3/

Note that a fully-working solution will need to bind this to more than just keyup events, because the user can paste with the context menu, for example. 请注意,完全工作的解决方案需要将其绑定到不仅仅是keyup事件,因为用户可以粘贴上下文菜单。 You may want to bind it to some mouse events or even run it occasionally on a timer if you find the box can be modified without triggering events (eg through a top level "edit" menu). 如果您发现可以修改框而不触发事件(例如通过顶级“编辑”菜单),您可能希望将其绑定到某些鼠标事件,甚至偶尔在计时器上运行它。

Check on every keypress if the content of your textarea is overflowing with the answer provided to this question: Determine if an HTML element's content overflows 检查每个按键是否textarea的内容溢出了这个问题的答案: 确定HTML元素的内容是否溢出

You'll then have to remove the extra characters and put them into the next textarea . 然后,您必须删除多余的字符并将它们放入下一个textarea Probably one by one, using your check overflow function after each character is moved to see if the text area is still overflowing . 可能是逐个,在移动每个字符后使用检查溢出功能查看文本区域是否仍然溢出

A possible solution is to measure the size of the text. 一种可能的解决方案是测量文本的大小。 Ext-JS does it by creating an offscreen absolutely positioned div, giving it style properties for the font you want to measure, and them measuring the div itself. Ext-JS通过创建一个绝对定位的div来实现它,为你想要测量的字体赋予它样式属性,并测量div本身。

You can use Ext-JS's code as inspiration http://docs.sencha.com/ext-js/4-1/#!/api/Ext.util.TextMetrics 您可以使用Ext-JS的代码作为灵感http://docs.sencha.com/ext-js/4-1/#!/api/Ext.util.TextMetrics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM