views:

250

answers:

5

I have this:

<a href="javascript:void(0);">
   <div>
      <span>some content</span>
      <span>some content</span>
   </div>
</a>

The problem is hovering the mouse over from one <span> to another triggers an instant mouseout and mouseover again even though they have no padding or margin between them. Even on the browser's status bar the link flickers for an instant. How can this be prevented?

+2  A: 

If you're using jQuery, you should switch to the hover method.

Otherwise, check e.target || e.srcElement.

SLaks
hover() works, I leave the first argument (hover-in) empty and use just the second (hover-out). mouseout() event still fires multiple times. The status bar still flickers the link though.
stagas
+1  A: 

Don't wrap block elements in anchor tags. Anchor tags are inline elements. If you are trying to use them as named locations for the URL hash (i.e., for internal page navigation), just put them above the content to which the page must scroll.

If you are trying to make something happen when you click on the div, put an onclick handler on the div.

Robusto
Unless he uses html5 doctype, where nesting block level elements inside of an `a` is permitted. Although I can only see that legitimately for forming a `link` rather than an anchor for in-page navigation.
David Thomas
doctype html5 didn't work. It still flickers..
stagas
A: 

Well, you do have a vertical gap (I can see a line feed between the two <span> tags). Remove it.

It's also worth noting that your HTML is far from valid. Inline elements like <a> cannot contain block elements like <div>.

Álvaro G. Vicario
OK that was just a quick example of a more complex html. The real code has no gaps and line-height is 0. The problem is mouseout fires even when using a div instead of anchor and the jQuery mouseout event. Using hover() with empty the first function() (entering element) and just using the second function() argument works as expected (no multiple hover-out events). Using the anchor still has problems with flickering the link in the status bar though.
stagas
Well, the mere fact of attaching events to DOM nodes does not cause flicker. Any change to see real code?
Álvaro G. Vicario
See my answer for an example that reproduces the problem exactly. Also have a link there
stagas
A: 

Here is a portion of the code that reproduces the problem, either you can copy & paste this to test it out or see here for the same thing I uploaded for testing :

<html><head><style>
a {
float:left;
border:1px solid #b0b;
display:block;
}
.shpf,.shpe {
background:#b0b; float:left;
width:2px; height:2px;
padding:0; margin:0;
line-height:0;
}
.shpe {
background:#fff;
}
.shpbrk {
float:left; clear:both;
}
</style></head><body>
<a id="happy" href="javascript:void(0);"><div id="happy_button" class="button"><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpf"></span><span class="shpf"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span></div></a>
</body></html>

Note: even using <!doctype html> which allows anchors to be block elements as ricebowl pointed out the result is the same. Hovering over the anchor triggers mouseout events and also makes the status bar to flicker the destination link text (at least in Firefox, didn't test elsewhere). The spans are empty which is wrong usually but the same happens even having a &nbsp; inside them.

stagas
A: 

Hacked my way to a solution. position:relative the anchor and a dummy div inside that expands at width and height 100% absolutely positioned to overlay the anchor exactly. No longer flickers the link on hovering and shouldn't be any dummy mouseout events firing. Here is the code and here is a link :

<html><head><style>
a {
float:left;
border:1px solid #b0b;
display:block;
position:relative;
}
.shpf,.shpe {
background:#b0b; float:left;
width:2px; height:2px;
padding:0; margin:0;
line-height:0;
}
.shpe {
background:#fff;
}
.shpbrk {
float:left; clear:both;
}
.dummy {
z-index:1;
position:absolute;
left:-1px; top:-1px; /* Fix for parent's border, isn't required for no border */
border:1px solid;
border-color:inherit;
background:transparent;
width:100%;
height:100%;
}
</style></head><body>
<a id="happy" href="javascript:void(0);"><div class="dummy"></div><div id="happy_button" class="button"><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpf"></span><span class="shpf"></span><span class="shpf"></span><span class="shpf"></span><span class="shpe"></span><span class="shpe"></span><span class="shpbrk"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span><span class="shpe"></span></div></a>
</body></html>
stagas