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

Javascript 如何在鼠标悬停时将焦点设置为输入元素(搜索框)?,javascript,html,mouseover,Javascript,Html,Mouseover,我想知道如何在mouseover事件中将焦点设置为输入字段 我想知道如何在onmouseover上加载输入字段或onfocus字段以便在光标触摸或onmouseover光标指向搜索框的输入字段时轻松地进行键入 使用jQuery: $('.input-element').mouseover(function() { $(this).focus(); }) 或者如果要控制焦点和模糊事件 $('.input-element') .mouseenter(function() {

我想知道如何在mouseover事件中将焦点设置为输入字段

我想知道如何在onmouseover上加载输入字段或onfocus字段以便在光标触摸或onmouseover光标指向搜索框的输入字段时轻松地进行键入

使用jQuery:

$('.input-element').mouseover(function() {
    $(this).focus();
})
或者如果要控制焦点和模糊事件

$('.input-element')
    .mouseenter(function() {
        $(this).focus();
    })
    .mouseleave(function() {
        $(this).blur();
    });
您可以使用该方法来聚焦HTML元素。下面是其用法的一个示例:

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

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

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

<div id='d1'>
<input type='text' id='txt' value='Search'/>
</div>
$().ready(function(){
  $('#d1').mouseover(function(){
   $(this).children().closest('Input[type=text]').focus();
  });
});