2016-12-07 44 views

回答

3

要聚焦元素使用focus()方法。在这里你尝试用一个不好的方法(看看Zakaria的答案)给所有输入添加属性autofocus。使用$(this)为目标悬停元素

$(document).ready(function(){ 
 
    $("input").hover(function(){ 
 
     $(this).focus(); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input value=""/><input value=""/><input value=""/>

+0

非常感谢Alexis。一旦限制通过,将在10分钟内接受。有没有办法切换焦点? –

+0

切换焦点是什么意思? – Alexis

+0

当光标不在输入上时,焦点被移除。 –

2

要添加属性元素,您应该使用.attr().prop()而不是appendTo

$(document).ready(function(){ 
    $("input").hover(function(){ 
    $(this).prop('autofocus'); 
    }); 
}); 

注意:autofocus属性不会使输入聚焦,而是focus()

希望这会有所帮助。

+1

你说得对,我不知道OP的真实意图是什么。 – Alexis

+1

@Alexis它看起来像他真的搜索焦点:) –

0

必须使用.focus();

$(document).ready(function(){ 
    $("input").hover(function(){ 
     $(this).focus(); 
     }); 
});