Keep div:hover open when changing nested select box

爷,独闯天下 提交于 2019-11-28 12:46:56
JakeParis

Solved this by adding a <a href="#" class="closeParentBox">close</a> to the .toolTip div and with

<!--[if IE]>
<script type="text/javascript">
  jQuery(function($){
    $('.toolTip select').focus(function(){
        $(this).parents('.toolTip').addClass('keepOpen');
    });
    $('.toolTip select').blur(function(){
        $(this).parents('.toolTip').removeClass('keepOpen');
    });

    $("a.closeParentBox").click(function(){
        $(this).parents('.toolTip').fadeOut();
        return false;
    });
  });
</script> 
<![endif]-->

Not pretty ... I'd love to hear better answers, though.

I have been able to isolate your issue into this simple demo: http://jsfiddle.net/TnzS4/

In the demo, you have a gray box that contains a SELECT element. Whenever you hover the box, it turns red.

Now, the modern browsers (Chrome, FF, Safari, Opera) bahave like so: if you open the SELECT element and hover over its items (OPTION elements), the DIV box will keep its red background - which means that the :hover state is still active.

However, if you hover over the OPTION elements in IE, the box turns gray - which means that the :hover state is not active.

Conclusion: In IE, hovering over a drop-down list of a SELECT element will "kill" all :hover states that were active.

Now, I don't know if this is a bug, but you will have to find a solution to this IE-specific issue. Unfortunately, I don't know the solution.

I can confirm. IE bug, from IE7 to IE9 9.0.7930.16406

I had a similar problem. The only way to resolve it, was using a Javascript to add/remove classes or changing the css properties on the fly!

I have updated Sime Vidas's JSFiddle, to include my solution: http://jsfiddle.net/TnzS4/221/

Here it is in a nutshell:

$wrap = $('.wrap');
$dropdown = $('.dropdown');

$dropdown.focus(function() {
  $wrap.addClass('hover');
  $dropdown.one('change', function() {
    $wrap.removeClass('hover');
    $dropdown.blur();
  });
});
$dropdown.blur(function() {
  $wrap.removeClass('hover');
});

I add a 'hover' class when the user is focused on the dropdown, and remove the 'hover' class when the user selects an option or loses focus on the dropdown.

The .hover class mimics the CSS of the :hover pseudo-class.

The 'focus' event only fires once when you click on the dropdown, until it is blurred again. The solution is to blur() the dropdown after selecting an option, allowing the fix to work multiple times.

In my particular case, I had to put the $wrap.removeClass('hover') within a setTimeout, as shown below. This depends on your scenario so I left it out of the JSFiddle:

setTimeout(function() {
  $wrap.removeClass('hover');
}, 1000);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!