Pregunta

I'm trying to build a website. I have it displaying correctly in every browser except IE versions 8 and lower. IE renders blue boxes around my img's that are also anchors. I can get rid of this by setting the border property to none but i'd like to do it with javascript. I can get my Javascript to execute in a conditional comment.

try
{
var ancs = document.getElementsByTagName("a");

    for(var i=0; i<ancs.length; i++)
    {
        if(ancs[i].childNodes[0].nodeName == "IMG")
        {
            //Set border property to none
        }
    }
}
catch(err)
{
alert(err);
}
¿Fue útil?

Solución

I am sorry for not answering to the javascript part. But you should do it with CSS like this:

a img { border:0; }

Otros consejos

What's your conditional comment look like? And, why not apply this as a style instead? It will be faster than doing it with JavaScript and better supported.

IE has a default border style for images that don't specify one themselves. This is a known pain of IE. The proper way to fix this is to add a default CSS rule to your page. If this is one of the first CSS rules, then it won't affect any other CSS rules you've already set:

<style type="text/css">
    img {border: none;}
</style>

or if you really only want to affect images that are in an <a> tag, you would use this CSS:

<style type="text/css">
    a img {border: none;}
</style>

If you only want to fix/change one image, you can also address that particular image in the <img> tag by specifying an inline border:

<img border="0" src="xxxx">

If you really want to do it with javascript, you can place this code either after your page has loaded or call it only once the page is loaded:

function nukeImageBorders() {
    // assumes all affected images have an <a> tag as their parent
    var list = document.getElementsByTagName("img");
    for (var i = 0, len = list.length; i < len; i++) {
        if (list[i].parentNode.tagName.toLowerCase() == "a") {
            list[i].style.border = "none";
        }
    }
}

You can see the code work in IE here: http://jsfiddle.net/jfriend00/cnEhY/

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top