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. One of the <div>s contains a <form> which, using jQuery, will keep the border if an input within it has focus. This works perfectly except that IE6 does not support :hover on any elements other than <a>s. So, for this browser only we are using jQuery to mimic CSS :hover using the $(#element).hover() method. 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.

我在想我们可以用一些条件来阻止这种行为。例如,如果我们在鼠标移出时测试任何输入是否有焦点,我们可以阻止边界消失。AFAIK,在jQuery中没有:focus选择器,所以我不确定如何做到这一点。什么好主意吗?


当前回答

跟踪这两个状态(悬停,聚焦)作为真/假标志,当其中一个发生变化时,运行一个函数,如果两者都为假,则删除边界,否则显示边界。

onfocus sets focused = true, onblur sets focused = false。Onmouseover sets hovers = true, onmouseout sets hovers = false。在每个事件之后运行一个添加/删除边框的函数。

其他回答

跟踪这两个状态(悬停,聚焦)作为真/假标志,当其中一个发生变化时,运行一个函数,如果两者都为假,则删除边界,否则显示边界。

onfocus sets focused = true, onblur sets focused = false。Onmouseover sets hovers = true, onmouseout sets hovers = false。在每个事件之后运行一个添加/删除边框的函数。

您是否考虑过使用mouseOver和mouseOut来模拟这种情况。还可以查看mouseEnter和mouseLeave

有一个插件可以检查一个元素是否被聚焦:http://plugins.jquery.com/project/focused

$('input').each(function(){
   if ($(this) == $.focused()) {
      $(this).addClass('focused');
   }
})

我不完全确定你在追求什么,但这听起来像它可以通过存储输入元素的状态(或div?)作为一个变量来实现:

$('div').each(function(){

    var childInputHasFocus = false;

    $(this).hover(function(){
        if (childInputHasFocus) {
            // do something
        } else { }
    }, function() {
        if (childInputHasFocus) {
            // do something
        } else { }
    });

    $('input', this)
        .focus(function(){
            childInputHasFocus = true;
        })
        .blur(function(){
            childInputHasFocus = false;
        });
});

简单的

 <input type="text" /> 



 <script>
     $("input").focusin(function() {

    alert("I am in Focus");

     });
 </script>