IE 修复 - 条件注释脚本不会执行
我正在尝试建立一个网站。我让它在除 IE 8 及更低版本之外的所有浏览器中正确显示。 IE 在我的 img 周围呈现蓝色框,这些框也是锚点。我可以通过将 border 属性设置为 none 来摆脱这个问题,但我想用 javascript 来做到这一点。我可以让我的 Javascript 在条件注释中执行。
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很抱歉没有回答 javascript 部分。但是你应该使用CSS来做到这一点,如下所示:
I am sorry for not answering to the javascript part. But you should do it with CSS like this:
你的条件评论是什么样的?而且,为什么不将其应用为一种样式呢?它会比使用 JavaScript 更快并且得到更好的支持。
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 对于本身未指定边框样式的图像有一种默认边框样式。这是 IE 众所周知的痛点。解决此问题的正确方法是向页面添加默认 CSS 规则。如果这是第一个 CSS 规则,那么它不会影响您已经设置的任何其他 CSS 规则:
或者如果您确实只想影响
中的图像标记,您可以使用此 CSS:
如果您只想修复/更改一张图像,您还可以通过指定内联边框来在
标记中处理该特定图像:
如果您确实想要要使用 javascript 执行此操作,您可以放置此代码可以在页面加载后调用,也可以仅在页面加载后调用:
您可以在此处查看代码在 IE 中的工作情况: http://jsfiddle.net/jfriend00/cnEhY/
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:
or if you really only want to affect images that are in an
<a>
tag, you would use this CSS: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: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:
You can see the code work in IE here: http://jsfiddle.net/jfriend00/cnEhY/