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

前端 未结 3 1510
挽巷
挽巷 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:37

    click() or focus() alone is not enough. You need to focus() then click(). Beware of endless loops if your script is triggered by an onclick() on a containing element. The script below is working for me on Chrome for android 58 and Safari mobile 602.1. Soft keyboard popping nicely.

    var target = document.getElementsByTagName("input")[0];
    
    if (event.target != target) {
        target.focus();
        target.click();
    }
    

提交回复
热议问题