views:

63

answers:

3

I have a simple div element that contains another div element which contains 2 inline tags. I've associated an onmouseout event to the first div container and an onmouseover event to the second container.

The problem is that the onmouseout event is fired when the user hovers their mouse between the two tags in the div and also after the end of the second tag.

What I want to do is allow the user to hover their mouse across the whole of the div tag and only fire the onmouseout event when the mouse pointer is outside the div element (which is what I assumed from what I've done).

I increased padding to close the gap between the 2 tags. This works but where they meet in IE7 at least the event is fired!!!

I must be doing something wrong can someone please help.

        <div id="Div1" onmouseout="hideDiv1()" >
            <div id="Div2" onmouseover="showDiv2()">
                <a id="A1" href="#">a</a>
                <a id="A2" href="#">b</a>
                <a id="A3" href="#">c</a>
            </div>
        </div>
A: 

Take a look at this to help you: A list apart's dropdown.

Kyle Sevenoaks
I tried using ul and li's but its exactly the same problem of having gaps between the inline elements.
insanepaul
If you're using as CSS dropdown menu, you can reduce the gaps using margins and padding.
Kyle Sevenoaks
I did that but in IE7 and probably IE6 when I hover over the top border around 2 or 3 pixels caused it to fire the onmouseevent. I've made a slight amendment to the code above which makes things clearer.
insanepaul
I understand your problem, I'm saying there are CSS rules that will show and hide the UL menu better than this and you won't encounter any weird misfirings. IE does have bugs, the resource I gave you has a JS to counter IE's bugs as well.
Kyle Sevenoaks
A: 

Have a look at this

<style>
.redBorder { border:1px solid red}
.blackBorder { border:1px solid black}
</style>
<script>
function doMouseout(e) { // code modified from "PointedEars"
  if (!e) e = window.event; // IE
  var
  // W3C DOM Level 2+ Events
  relatedTarget = e.relatedTarget,
  currentTarget = e.currentTarget;

  // MSHTML DOM
  if (!(relatedTarget && currentTarget)) {
    relatedTarget = e.toElement;
    currentTarget = e.srcElement;
  }

  if (relatedTarget && currentTarget && currentTarget.tagName.toLowerCase()=="div")  {
    hideBorder(currentTarget);
  }
}
function hideBorder(el) {
  el.className="redBorder"
}
function showBorder(el) {
  el.className="blackBorder"
}

</script>

<div class="redBorder" id="Div1" onmouseover="showBorder(this)" onmouseout="doMouseout(event)">
        <a id="A1" href="HTMLNew.htm">ARTICLES</a>
        <a id="A2" href="HTMLNew.htm">COURSES & CASES</a>
    </div>
</div>
mplungjan
Sheesh. one REALLY has to be fast to get a word in edgewise here !
mplungjan
You're not wrong, seems there's not a lot of people that usually answer yet either :P
Kyle Sevenoaks
+1  A: 

The 'gaps' are expected behaviour. These are inline elements with whitespace inbetween them, so the browser, rightly, renders a space in between them. If you want to close the gaps, the simplest, cleanest, most efficient, semantic approach is to actually close the gaps:

<div class="redBorder" id="Div1" onmouseover="showBorder(this)" onmouseout="doMouseout(event)">
    <a id="A1" href="HTMLNew.htm">ARTICLES</a><a id="A2" href="HTMLNew.htm">COURSES & CASES</a>
</div>

Although, you should also be aware that it's good accessibility practice to include characters between links.

graphicdivine
I've closed the gaps but in IE7 and probably IE6 when i hover close to the border it fires the onmouseout event. Whats the best way to clse the gaps?
insanepaul
When I say hover close to the border I mean hover close to the corner of the border so top right / top left / bottom left and bottom right
insanepaul
Can you give us a live demo of this somewhere? http://jsbin.com or somesuch?
graphicdivine