IE and Chrome don't fire a mouseover event for

2019-01-26 17:44发布

My code only works in Firefox. Why is this?

HTML:

<select id="selecter">
         <option>one</option>
         <option>two</option>
         <option>three</option>
   </select>

Javascript:

$(function() {
   $(document).on("mouseover", "#selecter option",function(){
            alert(1)        
    });
});

I'm curious why IE and chrome don't fire a mouseover event. See this JSFiddle: http://jsfiddle.net/yT6Y5/72/ (Works perfectly in Firefox.)

How can I get IE and Chrome to fire a mouseover event?

3条回答
倾城 Initia
2楼-- · 2019-01-26 17:45

maybe you should use , a different approach to bind the event

 $(function() {
    $("#selecter").mouseover(function(){
        alert(1)        
     });
  });
查看更多
何必那么认真
3楼-- · 2019-01-26 18:06

It seems, no events are actually fired when you hover over an option in IE & chrome,

At best should should bind on the change event.

$(function() {
    $("#selecter").change(function(){
            alert(1);
    });
});
查看更多
来,给爷笑一个
4楼-- · 2019-01-26 18:08

The problem is that browsers render dropdowns differently. Chrome is rendering it not as an HTML component but as a native GUI one. That can't have hover handlers associated to it from JS.

If you want to make sure it works on all browsers either don't use a dropdown or get a script to create a dropdown that uses HTML elements

查看更多
登录 后发表回答