JQuery - "|" the cursor is biased when focusing on the text field

I have a text box in my html package: input type="text" id="text-input"

I use this to focus it when the user clicks:

<script>
    $(document).keypress(function(e) {
        if(e.keyCode == 13) {
            $("#text-input").focus();
        }
    });
</script>

The webpage that I use to try has only a text box. When I click on an empty area and press enter, "|" the tip will be shifted down. If I repeat this click and press enter, the "|", which should be in the text box, continues to move more and more.

Something went wrong?

+3
source share
2 answers

Try using keyupinsteadkeypress

$(document).keyup(function(e) {
    if(e.keyCode == 13) {
        $("#text-input").focus();
    }
});​

EXAMPLE: keypress and KeyUp

Tested in chrome and the results are different.

: keypress = (keydown keyup 2). , , .

+1

, false :

$(document).keypress(function(e) 
{
    if(e.keyCode === 13) 
    {
        $("#text-input").focus();

        return false;
    }
});​

, e.preventDefault(); e.stopImmediatePropagation(); return false;, , , , .focus(), :

$(document).keypress(function(e) 
{
    if (e.keyCode === 13) 
    {
        $("#text-input").focus();

        e.preventDefault();
        e.stopImmediatePropagation();
    }
});

, e.stopImmediatePropagation(); , . , .focus(), - . , , , , , .

.

+1

All Articles