本文翻译自:Using jQuery to test if an input has focus
On the front page of a site I am building, several <div>
s use the CSS :hover
pseudo-class to add a border when the mouse is over them. 在我正在构建的站点的首页上,几个<div>
使用CSS :hover
伪类在:hover
时添加边框。 One of the <div>
s contains a <form>
which, using jQuery, will keep the border if an input within it has focus. 其中一个<div>
包含一个<form>
,使用jQuery,如果其中的输入具有焦点,它将保持边界。 This works perfectly except that IE6 does not support :hover
on any elements other than <a>
s. 这完全有效,除了IE6不支持:hover
在除<a>
的任何元素上。 So, for this browser only we are using jQuery to mimic CSS :hover
using the $(#element).hover()
method. 所以,对于这个浏览器,我们只使用jQuery来模仿CSS :hover
使用$(#element).hover()
方法:hover
。 The only problem is, now that jQuery handles both the form focus()
and hover()
, when an input has focus then the user moves the mouse in and out, the border goes away. 唯一的问题是,现在jQuery处理表单focus()
和 hover()
,当输入具有焦点然后用户移入和移出鼠标时,边框消失。
I was thinking we could use some kind of conditional to stop this behavior. 我以为我们可以使用某种条件来阻止这种行为。 For instance, if we tested on mouse out if any of the inputs had focus, we could stop the border from going away. 例如,如果我们测试鼠标输出是否有任何输入有焦点,我们可以阻