January 04th 2010

IE浏览器兼容性解决方案

一个优秀的网站要尽量兼容所有浏览器,但由于浏览器类型多样(IE 5, IE 6, IE7, IE 8, Firefox, Chrome, Safari),以及浏览器对CSS高版本的支持问题,这就给网页开发者带来诸多困难。

为了降低网页开发者寻找解决方案及编写代码的时间,我们收集了一些方法,以期能缩短开发时间,挺高网页在多浏览器中的兼容性。

IE浏览器兼容性解决方案

通过使用IE中的条件注释 (Conditional comments)。
条件注释只能用于IE 5以后版本的浏览器,其他类型的浏览器将会忽略此注释。
如果你安装了多个IE,条件注释(Conditional comments)将会以最高版本的IE为标准。

条件注释(Conditional comments)示例:

<!--[if IE]>
 <style>
  #logo {
          margin-left: 20px;
        }
</style>
<![endif]-->

条件注释(Conditional comments)说明:

  1. 条件注释的基本结构和HTML的注释(<!-- -->)是一样的。因此IE以外的浏览器将会把它们看作是普通的注释而完全忽略它们。
  2. IE将会根据if条件来判断是否如解析普通的页面内容一样解析条件注释里的内容。
  3. 条件注释使用的是HTML的注释结构,因此他们只能使用在HTML文件里,而不能在CSS文件中使用。

我们来测试一下条件注释(Conditional comments)的实际效果。

代码如下:

<!--[if IE]>
  根据条件判断,这是Internet Explorer<br />
< ![endif]-->
<!--[if IE 5]>
  根据条件判断,这是Internet Explorer 5<br />
< ![endif]-->
<!--[if IE 5.0]>
  根据条件判断,这是Internet Explorer 5.0<br />
< ![endif]-->
<!--[if IE 5.5]>
  根据条件判断,这是Internet Explorer 5.5<br />
< ![endif]-->
<!--[if IE 6]>
  根据条件判断,这是Internet Explorer 6<br />
< ![endif]-->
<!--[if gte IE 5]>
  根据条件判断,这是Internet Explorer 5 或者更高<br />
< ![endif]-->
<!--[if lt IE 6]>
  根据条件判断,这是版小于6的Internet Explorer<br />
< ![endif]-->
<!--[if lte IE 5.5]>
  根据条件判断,这是Internet Explorer 5.5或更低<br />
< ![endif]-->

注:gt代表大于, lte代表小于或等于。

预览 条件注释(Conditional comments)的实际效果

重置CSS各个元素的属性值

由于各个浏览器对CSS元素默认的属性值进行解析时,可能有所差异,所以我们尽量重置所需的CSS元素的属性值(CSS reset styles)。

我们常见如下所示的重置CSS默认属性值的代码:

* { margin: 0; padding: 0; }

但是仅对margin和padding重置也许远远不够,下面的代码对常用的CSS元素进行了重置。

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  font-weight: inherit;
  font-style: inherit;
  font-size: 100%;
  font-family: inherit;
  vertical-align: baseline;
}
/* remember to define focus styles! */
:focus {
  outline: 0;
}
body {
  line-height: 1;
  color: black;
  background: white;
}
ol, ul {
  list-style: none;
}
/* tables still need 'cellspacing="0"' in the markup */
table {
  border-collapse: separate;
  border-spacing: 0;
}
caption, th, td {
  text-align: left;
  font-weight: normal;
}
blockquote:before, blockquote:after,
q:before, q:after {
  content: "";
}
blockquote, q {
  quotes: "" "";
}

ps.我转载的.....




如果您喜欢本篇文章,也许您也会喜欢下面推荐的文章!

在线RSS阅读器订阅:
feedsky
抓虾 pageflakes Rojo
google reader my yahoo newsgator
bloglines 有道

手机订阅:


本站订阅地址:
RSS2:点击复制
Atom:点击复制

为了保护版权,尊重别人的劳动成果请遵循创作共同协议


请您在转载本站文章时在醒目的位置注明该文出处并附上本站的链接.


如果使用本站皮肤,请在底部栏注明作者(Tyiao)和作者链接(www.tyiao.com)


鸟语地带


评论: 0 | 引用: 0 | 查看次数: - | TOP ↑
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.