简体   繁体   English

jQuery / Javascript:IE悬停不覆盖选择框选项吗?

[英]JQuery/Javascript: IE hover doesn't cover select box options?

I have this bit of script to widen a text box on mouseover and shorten it on mouseoff. 我有这段脚本可以在鼠标悬停时加宽文本框,在鼠标悬停时将其缩短。

The problem I am having is that Internet Explorer doesn't seem to extend it's hover over the options of a select box. 我遇到的问题是Internet Explorer似乎没有扩展,而是将其悬停在选择框的选项上。

This means in IE I can click the select, have the options drop down, but if I try to select one, they vanish and the select box re-sizes as soon as I move off the select box itself. 这意味着在IE中,我可以单击选择,下拉选项,但是如果我尝试选择一个,它们就会消失,并且一旦我移开选择框本身,选择框就会重新调整大小。

Example Code: 示例代码:

<script type='text/javascript'>
$(function() {
$('#TheSelect').hover(
    function(e){
        $('#TheText').val('OVER');
        $(this).width( 600 );
    },
    function(e){
        $('#TheText').val('OUT');
        $(this).width( 50 );
   }
);
});
</script>

And: 和:

<input type='text' id='TheText' /><br /><br />

<select id='TheSelect' style='width:50px;'>
    <option value='1'>One</option>
    <option value='2'>Two</option>
    <option value='3'>Three</option>
    <option value='42,693,748,756'>Forty-two billion, six-hundred and ninety-three million, seven-hundred-forty-some-odd..... </option>
    <option value='5'>Five</option>
    <option value='6'>Six</option>
    <option value='7'>Seven...</option>
</select>

Are there any workarounds for select boxes in IE? IE中的选择框是否有任何变通办法? I would even consider a jquery replacement if anyone can recommend one that is really reliable. 如果有人可以推荐一个真正可靠的替代品,我什至会考虑用一种替代品。

Thanks! 谢谢!

Looks like this post has been up for a while, but hopefully there are still folks interested in a workaround. 看起来这篇文章已经有一段时间了,但是希望仍然有一些人对解决方法感兴趣。 I experienced this issue while building out a new site that I'm working on. 在建立我正在处理的新网站时,我遇到了这个问题。 On it is a product slider, and for each product, mousing over the product pops up a large informational bubble with info about the product, a dropdown to select buy options, and a buy button. 在它上面是一个产品滑块,对于每个产品,将鼠标悬停在该产品上会弹出一个大的信息气泡,其中包含有关该产品的信息,用于选择购买选项的下拉菜单和一个购买按钮。 I quickly discovered that anytime my mouse left the initial visible area of the select menu (ie, trying to select an option), the entire bubble would disappear. 我很快发现,只要鼠标离开选择菜单的初始可见区域(即尝试选择一个选项),整个气泡就会消失。

The answer (thank you, smart folks that developed jQuery), was all about event bubbling. 答案(感谢开发jQuery的聪明人)完全是关于事件冒泡的问题。 I knew that the most straightforward way to fix the issue had to be temporarily "disabling" the out state of the hover. 我知道解决此问题的最直接方法必须是暂时“禁用”悬停状态。 Fortunately, jQuery has functionality built in to deal with event bubbling (they call it propagation). 幸运的是,jQuery具有内置功能来处理事件冒泡(他们称其为传播)。

Basically, with only about a line or so of new code, I attached a method to the "onmouseleave" event of the dropdown (mousing over one of the options in the select list seems to trigger this event reliably - I tried a few other events, but this one seemed to be pretty solid) which turned off event propagation (ie, parent elements were not allowed to hear about the "onmouseleave" event from the dropdown). 基本上,只有大约一行新代码,我为下拉菜单的“ onmouseleave”事件附加了一个方法(将鼠标悬停在选择列表中的一个选项上似乎可以可靠地触发此事件-我尝试了其他一些事件,但此功能似乎非常可靠),从而关闭了事件传播功能(即,不允许父元素从下拉列表中听到“ onmouseleave”事件)。

That was it! 就是这样! Solution was much more elegant that I expected it to be. 解决方案比我期望的要优雅得多。 Then, when the mouse leaves the bubble, the out state of the hover triggers normally and the site goes on about its business. 然后,当鼠标离开气泡时,悬停的退出状态将正常触发,并且该站点将继续其业务。 Here's the fix (I put it in the document.ready): 解决方法如下(我将其放在document.ready中):

$(document).ready(function(){
  $(".dropdownClassName select").mouseleave(function(event){
    event.stopPropagation();
  });
});

Apparently IE doesn't consider the drop down bit part of the select element. 显然,IE不考虑select元素的下拉位部分。 It's doable, but it takes a bit of cheating with expando properties and blur/focus events to enable and disable the 'hide' effect to stop it kicking in when the mouse enters the drop-down part of the element. 它是可行的,但是它需要对expando属性和模糊/焦点事件进行一些欺骗,才能启用和禁用“隐藏”效果,以在鼠标进入元素的下拉部分时阻止其进入。

Have a go with this: 尝试一下:

$(function() {
    var expand   = function(){ $(this).width(600) }
    var contract = function(){ if (!this.noHide) $(this).width(50) }
    var focus    = function(){ this.noHide = true }
    var blur     = function(){ this.noHide = false; contract.call(this) }
    $('#TheSelect')
        .hover(expand, contract)
        .focus(focus)
        .click(focus)
        .blur(blur)
        .change(blur)
});

(Apologies if this isn't how one is supposed to use jQuery - I've never used it before :)) (很抱歉,如果这不是应该使用jQuery的方式,我以前从未使用过它:))

I had a form that was hidden/shown based on hover. 我有一个基于悬停隐藏/显示的表单。 If the user focused on the select the form became hidden. 如果用户专注于选择,则表单将隐藏。 I was able to solve my problem with something like this: 我可以通过以下方式解决我的问题:

element.find('select').bind('mouseenter mouseleave',function(){
    //Prevent hover bubbling
    return false;
});

WorldWideWeb's answer worked perfectly. WorldWideWeb的答案非常有效。

I had a slighty different problem, I had a hover effect on the containing item (hover to reveal a select menu) of the form field and on IE users couldn't pick from the drop down (it kept resetting the value). 我有一个稍微不同的问题,我对表单字段的包含项(将鼠标悬停以显示选择菜单)产生悬停效果,并且对IE用户无法从下拉菜单中进行选择(它一直在重置值)。 I added worldwideweb's suggestion (with the ID of the select) and viola, it worked. 我添加了Worldwideweb的建议(带有选择的ID)和中提琴,它奏效了。

HEre's what I did: 这是我所做的:

$(".containerClass").hover(function() {
    $(this).children("img").hide();
    $('#idOfSelectElement').mouseleave(function(event) { event.stopPropagation(); });
}, function() {
    $(this).children('img').show();
    $('#idOfSelectElement').mouseleave(function(event) { event.stopPropagation(); });

});

Had the same problem, and WorldWidewebb's answer worked very well on IE8. 遇到了同样的问题,WorldWidewebb的答案在IE8上效果很好。 I just made a slight change, removing "select" from the selector, since I included the select box's class in the selector. 我做了一点改动,从选择器中删除了“选择”,因为我在选择器中包括了选择框的类。 Made it a really simple fix. 使其变得非常简单。

Also, i had it implemented on a menu that uses the hoverIntent jquery plugin, with no problems. 另外,我在使用hoverIntent jquery插件的菜单上实现了它,没有任何问题。 (No interference). (无干扰)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM