jQuery handing both focus and click on an element

前端 未结 3 1831
暗喜
暗喜 2020-12-09 11:28

I\'m trying to determine workflow to fine-tune a data entry web application. Picture several address forms on a single web page:

  1. Name___________
     S         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 12:22

    Just bind the focus and then check the event to see if e.which == 9 // tab.

    My original idea did not work (thanks @Juan). Using the combination of events should get you where you want to be. If the user clicks into the text box the last key press will not be a tab. All inputs are watching and storing the last key code to pass to the focus event. here is the fiddle

    var lastKeyCode = 0;
    $('input').focus(function(e) {
        if(lastKeyCode == 9)
             // tabbed into field
        else
             // clicked into field
    }).keydown(function(e){
        if(e.which == 9) // added timeout to clear lastkeypress
            setTimeout(function(){lastKeyPress = 0}, 50);
        lastKeyPress = e.which; // store last key code
    });
    

提交回复
热议问题