0

(id="center") を含む div にタブ移動した後、テキストエリア内に入力できませんか?

これがコードです...

 <div id="north">North</div>
 <div id="west">West</div>
 <div id="center"><textarea></textarea></div> 

脚本

$(document).ready(function() {
    var divs = ["north", "west", "center"];
    var startIndex = 0;
    $(document).keydown(function(e) {
        if (e.which == 9) {
            $("div").css("border", "");
            $("#" + divs[startIndex]).css("border", "4px solid gray");
            startIndex++;
            if (startIndex === divs.length) {
                startIndex = 0;
            }
        }
        return false;
    });
});​
4

2 に答える 2

1

キーダウン時にfalseを返しています-これにより、テキストエリア内に入力できなくなります

$(document).keydown(function(e) {
    if (e.which == 9) {
        $("div").css("border", "");
        $("#" + divs[startIndex]).css("border", "4px solid gray");
        startIndex++;
        if (startIndex === divs.length) {
            startIndex = 0;
        }
    }
    return false; // <--
});

デフォルトのタブの動作を防ぎたい場合は、if (e.which == 9)ステートメント内に移動する必要があります

$(document).keydown(function(e) {
    if (e.which == 9) {
        $("div").css("border", "");
        $("#" + divs[startIndex]).css("border", "4px solid gray");
        startIndex++;
        if (startIndex === divs.length) {
            startIndex = 0;
        }
        return false; // <-- Move it here to prevent tab default behavior
    }        
});

または、キーダウン時のデフォルトの動作を防ぐ必要がない場合は、完全に削除できます

于 2012-10-19T20:06:30.533 に答える
0

メソッドを使用して.preventDefault()、デフォルトのイベントを停止できます。

$(document).ready(function() {
    var divs = ["north", "west", "center"];
    var startIndex = 0;
    $(document).keydown(function(e) {
        if (e.which == 9) {
            e.preventDefault()
            $("div").css("border", "");
            $("#" + divs[startIndex]).css("border", "4px solid gray");
            startIndex++;
            if (startIndex === divs.length) {
                startIndex = 0;
            }
        }

    });
});
于 2012-10-19T20:51:08.070 に答える