【问题标题】:How to set focus to an input element (searchbox) on mouseover?如何在鼠标悬停时将焦点设置到输入元素(搜索框)?
【发布时间】:2017-02-15 12:06:00
【问题描述】:

我想知道如何在鼠标悬停事件上将焦点设置到输入字段。

我想知道如何在鼠标悬停时加载输入字段或焦点字段以进行输入,以便在光标触摸或鼠标悬停光标到搜索框的输入字段时轻松输入。

【问题讨论】:

    标签: javascript html mouseover


    【解决方案1】:

    使用 jQuery:

    $('.input-element').mouseover(function() {
        $(this).focus();
    })
    

    或者如果你想控制焦点和模糊事件

    $('.input-element')
        .mouseenter(function() {
            $(this).focus();
        })
        .mouseleave(function() {
            $(this).blur();
        });
    

    【讨论】:

      【解决方案2】:

      您可以使用focus() 方法来聚焦HTML 元素。以下是它的用法示例:

      var inputField = document.getElementById('idOfYourInputField');
      
      inputField.addEventListener('mouseover', function() {
          inputField.focus();
      });
      

      【讨论】:

        【解决方案3】:

        不确定您在这里期待什么,但是如果您希望在用户移动鼠标时在页面上的某个位置出现类似内容,您希望文本框获得焦点,您可以执行以下操作:

        <div id='d1'>
        <input type='text' id='txt' value='Search'/>
        </div>
        

        并使用 Jquery:

        $().ready(function(){
          $('#d1').mouseover(function(){
           $(this).children().closest('Input[type=text]').focus();
          });
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-15
          • 1970-01-01
          • 1970-01-01
          • 2011-11-08
          • 2018-04-02
          • 1970-01-01
          • 2012-11-03
          相关资源
          最近更新 更多