将焦点设置为输入字段

sch*_*omm 2 html jquery

我正在尝试将焦点设置到此Leaflet插件输入字段的搜索栏,但它不起作用.我尝试过使用以下内容:

//jquery  
$("#leaflet-control-geosearch-qry").focus();  
//html  
document.getElementById("leaflet-control-geosearch-qry").focus();
Run Code Online (Sandbox Code Playgroud)

你可以自己试试.不会将焦点设置为使用上述行设置搜索栏.

将其中一个映射到一个按钮并单击该按钮会将焦点设置到搜索栏.

<script>
    //Set Focus to Search Box
    function setFocusToSearchBar(){
        document.getElementById("leaflet-control-geosearch-qry").focus();
        //or $("#leaflet-control-geosearch-qry").focus();  
    }
</script>


<div id="Focus_on_Searchbar">
    <button id="sidebar_button_focus" onclick="setFocusToSearchBar();">
        Focus on Searchbar
    </button>
</div>
Run Code Online (Sandbox Code Playgroud)

这有效!使用其中一个与Chrome/FF控制台不起作用,我不知道为什么不这样做,因为它适用于一个按钮.

有任何想法吗?我正在使用Bootstrap作为我的Header-Bar ...也许这很重要?

Edg*_*ado 10

有用.发生的事情是,当您的控制台打开时,控制台具有焦点.

在控制台上键入:

setTimeout(function(){
    document.getElementById("leaflet-control-geosearch-qry").focus();
}, 5000);  //Will deffer execution 5 seconds
Run Code Online (Sandbox Code Playgroud)

然后关闭控制台.5秒后,它将聚焦.所以,这不是编码问题,不要担心;)

干杯