使得输入文本字段保持焦点而不是在我点击其他内容时丢失(要始终关注输入id ="input_message")?

Pao*_*aJ. 0 html javascript css

我在一个文本字段(输入消息)和发送按钮之上进行了简单的网络聊天,bubles(消息).如何使输入文本字段保持焦点而不是在我点击其他内容时丢失(要始终关注输入id ="input_message")?

Jos*_*ber 10

var el = document.getElementById('input_message');

el.focus();

el.onblur = function () {
    setTimeout(function () {
        el.focus();
    });
};
Run Code Online (Sandbox Code Playgroud)

这是小提琴:http://jsfiddle.net/MwaNM/


inh*_*han 5

这是一个肮脏的黑客.

<input type="text" id="input_message" />
<script type="text/javascript">
    with (document.getElementById('input_message')) {
        onblur = function(e) {
            var elm = e.target;
            setTimeout(function(){elm.focus()});
        }
        onkeydown = function(e) {
            var key = e.which || e.keyCode;
            if (key == 9) e.preventDefault();
            // code for tab is 9
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)