最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - to make that input text field holds focus and not to lose when I click on something else (to focus always be on inp

programmeradmin3浏览0评论

I made simple web chat, bubles ( messages) above one text field (input message) and send button. How to make that input text field holds focus and not to lose when I click on something else (to focus always be on input with id="input_message") ?

I made simple web chat, bubles ( messages) above one text field (input message) and send button. How to make that input text field holds focus and not to lose when I click on something else (to focus always be on input with id="input_message") ?

Share Improve this question asked Nov 26, 2012 at 21:19 PaolaJ.PaolaJ. 11.6k23 gold badges78 silver badges114 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10
var el = document.getElementById('input_message');

el.focus();

el.onblur = function () {
    setTimeout(function () {
        el.focus();
    });
};

Here's the fiddle: http://jsfiddle/MwaNM/

Here's a dirty hack.

<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>
var inputElement = document.getElementById("input_message");
    inputElement.focus();
    inputElement.addEventListener("blur", function(event){
        inputElement.focus();
    }); 

http://jsfiddle/653w1mpv/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论