如何在鼠标悬停时将焦点设置为输入元素(搜索框)?

时间:2016-10-06 16:25:46

标签: javascript html mouseover

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

我想知道如何在onmouseover上输入字段或onfocus字段进行输入,以便在光标触摸或onmouseover光标到搜索框的输入字段时可以轻松输入。

3 个答案:

答案 0 :(得分:1)

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

var inputField = document.getElementById('idOfYourInputField');

inputField.addEventListener('mouseover', function() {
    inputField.focus();
});

答案 1 :(得分:0)

不确定你在这里期待什么,但是如果你想在页面上的某个位置,如果用户移动鼠标你希望文本框获得Focus,你可以做类似的事情:

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

和Jquery:

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

答案 2 :(得分:-1)

使用jQuery:

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

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

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