简体   繁体   中英

ASP.NET / HTML - textarea cursor position when using cursors

In "modern" web browsers, when users press the up- and down-arrow keys in multi-line inputs, the cursor moves up and down. When on first line, pressing the up-arrow makes the cursor back to the begin of the line. That's standard behavior in FF, Chrome and so on.

In IE7, it's different. When the cursor is on first line, pressing the up-arrow does nothing. The cursor is still in the same position.

I want IE7 behavior in all browsers. How can I achieve this? Thanks in advance.

This will mean quite a bit of work and adjusting, but you may be able to use jQuery Caret , a jQuery plug-in.

You can use it to get as well as set your cursor position.

First of all, you can catch the keydown event in your textarea

<textarea id="myTextArea" value="some text" ></textarea>

with jQuery while checking it's the up arrow using

$("#myTextArea").bind("keydown", function(e) {
    var code = e.keyCode || e.which;
    if(code == 38) { // Arrow up
        .... code here ....
    }
});

Then you should use Caret to write code that 1. remembers the last cursor position, and 2. if the arrow up key is hit and the resulting position is 0, you reset the cursor position to the last remembered one. And then write similar code for the bottom/down part.

Sorry I can't program it out, but something like this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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