Mouseover event doesn't granulate on IE9 for sub elements, event doesn't start on IE8

前端 未结 5 488
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 01:03

We were adapting a method posted here highlight a DOM element on mouse over, like inspect does to NOT use jQuery.

We came up with this solution so fa

5条回答
  •  孤城傲影
    2021-01-02 01:33

    It turns out that in IE, elements that have no background (i.e. background: transparent) and the Gradient filter set do not receive mouse events. Demo

    This is a happy coincidence, since you're using a RGBa background colour for your overlay and one of the workarounds for RGBa colours in IE is the Gradient filter.

    By setting these styles on the overlay (for IE):

    background: transparent;
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#7F000000,endColorstr=#7F000000)"; /* IE8 */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7F000000,endColorstr=#7F000000);   /* IE6 & 7 */
    zoom: 1;
    

    mouse events pass through the overlay and onto the underlying elements, so inner / minor elements are highlighted correctly.

    Other issues that are present in IE7/8:

    • When using element.attachEvent, the event name needs to be prefixed with "on":

      document.body.attachEvent('onmouseover', function(e) { ... })
      
    • To find the target of the event, you need to access event.srcElement instead of event.target.

    • As rodneyrehm mentioned, Array.indexOf isn't supported.

    So here's a version of your solution that also works in IE 7-9: http://jsfiddle.net/jefferyto/Q7ZQV/7/

    (BTW The highlighting is wrong for inline elements that span more than one line, e.g. the "ask your own question" link in the "Browse other questions..." line.)

提交回复
热议问题