1

在 html textarea 输入中,我希望选项卡键盘按钮的作用类似于文字处理器样式的缩进键,而不是跳到下一个元素。

如何才能做到这一点?

4

1 回答 1

1

谷歌是你的朋友!关联

    function insertTab(o, e)
{
    var kC = e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which;
    if (kC == 9 && !e.shiftKey && !e.ctrlKey && !e.altKey)
    {
        var oS = o.scrollTop;
        if (o.setSelectionRange)
        {
            var sS = o.selectionStart;
            var sE = o.selectionEnd;
            o.value = o.value.substring(0, sS) + "\t" + o.value.substr(sE);
            o.setSelectionRange(sS + 1, sS + 1);
            o.focus();
        }
        else if (o.createTextRange)
        {
            document.selection.createRange().text = "\t";
            e.returnValue = false;
        }
        o.scrollTop = oS;
        if (e.preventDefault)
        {
            e.preventDefault();
        }
        return false;
    }
    return true;
}

<textarea onkeydown="insertTab(this, event);"></textarea>
于 2012-11-30T10:58:11.457 回答