How to focus an input field on Android browser through javascript or jquery

前端 未结 3 1514
挽巷
挽巷 2020-12-01 22:12

I\'ve tried $(\'#field\').focus(), and any other method found on the internet. Nothing worked. I have a simple html that reproduces the problem.



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

    if you bind it to another click event it will work. This works for me:

        $(document).ready(function()
        {
    
           $('#field').click(function(e){ $(this).focus(); });
    
                $('body').click(function(e)
                {
                    $('#field').trigger('click');
                })
        })   
    

    Will pop up the software keyboard. trigger() will trigger any event you give it. In this case the default behaviour of clicking on the field == tap == focus == win! Note: this call is bound to another click event happening.

提交回复
热议问题