单击div类的href按钮时如何关注输入元素

时间:2019-12-09 13:43:37

标签: javascript html input focus element

 <ul id="top_menu">
  <li>
    <a href="#" id="arabutton" class="search-overlay-menu-btn" ><?php echo $pages->get('/')->text36;?></a>
    </li>
</ul>



        <!-- Search Menu -->
        <div class="search-overlay-menu">

                    <form  id="form-game_v2" name="form-game_v2" method="get" action="<?php $pages->get("template=home")->url ?>" >
                        <div class="typeahead__container">
                            <div class="typeahead__field">
                                <div class="typeahead__query">
                                    <input  class="js-typeahead-game_v2" id="ara" name="form" type="search" placeholder="Search"  autofocus>
                                </div>
                                <div class="typeahead__button">
                                    <button type="submit">
                                        <i class="typeahead__search-icon"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>
        </div><!-- End Search Menu -->

我需要关注输入id =“ ara” 当点击id =“ arabutton”

我该怎么做?它是一个实时搜索系统。当单击按钮覆盖时,输入字段将显示为类型。

实时示例:http://medilocation.com/en/treatments/pediatric-cardiology/ 单击右上角进行搜索,就会看到该焦点问题。

4 个答案:

答案 0 :(得分:1)

您应该使用JavaScript:

document.addEventListener('DOMContentLoaded', () => {
  const input = document.getElementById('ara');
  const button = document.getElementById('arabutton');

  button.addEventListener('click', () => {
    input.focus();
  })
})

答案 1 :(得分:0)

const btn = document.querySelector('#arabutton');
const input = document.querySelector('#ara'); // or getElementById

btn.addEventListener('click', () => {
   input.focus();
});

上尉明显吗?

答案 2 :(得分:0)

使用javascript事件

<script>
  function focus(event) {
      e.preventDefalt() //to stop the redirection effect
      document.getElementById("ara").focus()
  }
 </script>
 <a onclick="focus()" id="arabutton" class="search-overlay-menu-btn">page</a>
 <input class="js-typeahead-game_v2" id="ara" name="form" type="search" placeholder="Search" autofocus>

答案 3 :(得分:0)

我终于做到了

<script>
 document.getElementById("arabutton").addEventListener("click", function(){
     setTimeout(function() { jQuery('#ara').focus() }, 20);
});
</script>

arabutton是按钮的ID。单击它时会出现重叠表格。 ara是叠加表单上inputfield的ID