Keypress en jQuery: Appuyez sur l'ONGLET à l'intérieur de la balise TEXTAREA (lors de la modification d'un texte existant)

Je veux insérer des caractères de TABULATION à l'intérieur d'un TEXTAREA, comme ceci:

<textarea>{KEYPRESS-INSERTS-TAB-HERE}Hello World</textarea>

Je peux insérer avant/après l'existant TEXTAREA texte - et je peux insérer /remplacer tout le texte dans le TEXTAREA - mais n'ont pas encore été en mesure d'insérer à l'intérieur de l'existant TEXTAREA texte (par le curseur) d'une manière simple.

$('textarea:input').live('keypress', function(e) {
    if (e.keyCode == 9) {
        e.preventDefault();

        //Press TAB to append a string (keeps the original TEXTAREA text).
        $(this).append("TAB TAB TAB AFTER TEXTAREA TEXT");

        //Press TAB to append a string (keeps the original TEXTAREA text).
        $(this).focus().prepend("TAB TAB TAB BEFORE TEXTAREA TEXT");

        //Press TAB to replace a all text inside TEXTAREA.
        $(this).val("INSERT INTO TEXTAREA /REPLACE EXISTING TEXT");

    }
});

Il y a un "onglets dans le textarea" plug-in jQuery ("Tabby") - mais c'est 254 lignes de code - j'espérais juste quelques lignes de code.

Quelques liens que j'ai étudié: (encore une fois, je préfère moins de lignes de code).

http://www.dynamicdrive.com/forums/showthread.php?t=34452

http://www.webdeveloper.com/forum/showthread.php?t=32317

http://pallieter.org/Projects/insertTab/

Veuillez en informer. Merci.

Le lien pour le Tabby est mort, mais un dépôt GitHub est disponible: github.com/alanhogan/Tabby

OriginalL'auteur Kristoffer Bohmann | 2009-11-15