I'm working with the superfish menu and had to replace all background image changes to <img src>
changes due to some accessibility guidelines I am forced to follow. So the menu is made up entirely of images with text in them.
In IE, when you click an image, it briefly changes to a red X image before following the link, and I need to get rid of that somehow and I'm stumped. I've never experienced this before.
var thisImg = null,
newImg = null,
imgSrc = null;
$(".sf-menu li").bind('mouseenter focusin', function () {
if ( $(this).hasClass('sf-breadcrumb') ) {
return;
}
else {
thisImg = $(this).find("img:first"),
newImg = thisImg.attr("src").replace(/.png/, "-hover.png");
if ( $(this).is(".sf-menu>li>ul>li") ) {
thisImg.attr("src", newImg);
}
else {
$(".sf-breadcrumb").find("img:first").attr("src", function(i,s){ return s.replace(/-hover.png/, ".png"); });
thisImg.attr("src", newImg);
}
}
});
$(".sf-menu li").bind('mouseleave focusout', function () {
if ( $(this).hasClass('sf-breadcrumb') ) {
return;
}
else {
thisImg = $(this).find("img:first"),
newImg = thisImg.attr("src").replace(/-hover.png/, '.png');
if ( $(this).is(".sf-menu>li>ul>li") ) {
thisImg.attr("src", newImg);
}
else {
$(".sf-breadcrumb").find("img:first").attr("src", function(i,s){ return s.replace(/.png/, "-hover.png"); });
thisImg.attr("src", newImg);
}
}
});
EDIT: I'm testing in IE8. "Red X" I figured was ubiquitous: In IE, an image which didn't load from the src. It displays a black border with a red X image in the upper left and displays the alt text. It seems like it is executing the mouseenter focusin event handler and adding another -hover.png to the src when you click as well. So the src is changing on click to imgName-hover-hover.png
Another Edit! So focusin event handler is being fired when clicked.
EDIT: For anyone wondering, IE and FF were handling clicks as focus events, so this was executing the mouseenter handler and the focusin handler. Heres my new code:
var thisImg = null,
newImg = null,
thatImg = null;
$(".sf-menu li").on('mouseenter', function () {
if ( $(this).hasClass('sf-breadcrumb') ) {
return;
}
else {
thisImg = $(this).find("img:first"),
newImg = thisImg.attr("src").replace(/.png/, "-hover.png");
if ( $(this).is(".sf-menu>li>ul>li") ) {
thisImg.attr("src", newImg);
}
else {
$(".sf-breadcrumb").find("img:first").attr("src", function(i,s){ return s.replace(/-hover.png/, ".png"); });
thisImg.attr("src", newImg);
}
}
});
$(".sf-menu li").on('mouseleave', function () {
if ( $(this).hasClass('sf-breadcrumb') ) {
return;
}
else {
thisImg = $(this).find("img:first"),
newImg = thisImg.attr("src").replace(/-hover.png/, '.png');
if ( $(this).is(".sf-menu>li>ul>li") ) {
thisImg.attr("src", newImg);
}
else {
$(".sf-breadcrumb").find("img:first").attr("src", function(i,s){ return s.replace(/.png/, "-hover.png"); });
thisImg.attr("src", newImg);
}
}
});
$(".sf-menu a").focus( function() {
thisImg = $(this).find("img:first"),
newImg = thisImg.attr("src").replace(/.png/, "-hover.png");
if (thisImg.attr("src") == thatImg.attr("src")) {
return;
}
else if ( $(this).parent("li").hasClass('sf-breadcrumb') ) {
return;
}
else {
if ( $(this).is(".sf-menu>li>ul>li") ) {
thisImg.attr("src", newImg);
}
else {
$(".sf-breadcrumb").find("img:first").attr("src", function(i,s){ return s.replace(/-hover.png/, ".png"); });
thisImg.attr("src", newImg);
}
thatImg = thisImg;
}
});
$(".sf-menu a").blur( function() {
thisImg = $(this).find("img:first"),
newImg = thisImg.attr("src").replace(/-hover.png/, '.png');
if (thisImg.attr("src") == thatImg.attr("src")) {
return;
}
else if ( $(this).parent("li").hasClass('sf-breadcrumb') ) {
return;
}
else {
if ( $(this).is(".sf-menu>li>ul>li") ) {
thisImg.attr("src", newImg);
}
else {
$(".sf-breadcrumb").find("img:first").attr("src", function(i,s){ return s.replace(/.png/, "-hover.png"); });
thisImg.attr("src", newImg);
}
thatImg = thisImg;
}
});
I'm sure this could be cleaned up in some way, but at least it's working.