HTML 5兼容IE的写法

本文详细解读了如何通过X-UA-Compatible元标签实现IE8的兼容性优化,包括其可用方法和Emulate模式对DOCTYPE的影响,以及推荐的首选方法。

来作为IE8的兼容方法,虽然微软将IE向标准迈进了一大步,而事实上IE8还存在一系列渲染的奇怪现象是不争的事实。谁让IE6那么多呢,或许2014年以后我们可以有更多的时间去关心IE8,而不是IE6或者IE7。

 
  1. <!DOCTYPE html> 
  2. <!–[if IE]> 
  3. <meta http-equiv=”X-UA-Compatible” content=”IE=8″ /> 
  4. <![endif]–> 
  5. <!–[if IE 7]> 
  6. <meta http-equiv=”X-UA-Compatible” content=”IE=7″ /> 
  7. <![endif]–> 
  8. <!–[if IE 6]> 
  9. <meta http-equiv=”X-UA-Compatible” content=”IE=6″ /> 
  10. <![endif]–> 

关于X-UA-Compatible

目前绝大多数网站都用

 
  1. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" > 

来作为IE8的兼容方法,虽然微软将IE向标准迈进了一大步,而事实上IE8还存在一系列渲染的奇怪现象是不争的事实。

谁让IE6那么多呢,或许2014年以后我们可以有更多的时间去关心IE8,而不是IE6或者IE7。

在X-UA-Compatible中可用的方法有:

 
  1. <meta http-equiv="X-UA-Compatible" content="IE=5" > 
  2. <meta http-equiv="X-UA-Compatible" content="IE=7" > 
  3. <meta http-equiv="X-UA-Compatible" content="IE=8" > 
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge" > 

其中最后一行是永远以最新的IE版本模式来显示网页的。

另外加上

 
  1. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" > 
  2. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" > 

而使用,Emulate模式后则更重视<!DOCTYPE>

所以目前来说还是以

  
  1. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" > 

为首选。

微软自己创下的罪孽让他自己去扫吧。

原文链接:http://developer.51cto.com/art/201205/334524.htm

### 解决 `position: sticky` 在 Internet Explorer 中的兼容性 Internet Explorer 并不支持 CSS 的 `position: sticky` 属性。为了实现类似的效果,可以采用 JavaScript 或 jQuery 插件作为 polyfill 来模拟该行为。 一种常见的做法是监听滚动事件并动态调整元素的位置属性: ```javascript (function($) { $.fn.sticky = function(options) { var settings = $.extend({ topSpacing: 0, bottomSpacing: 0 }, options); return this.each(function() { var $this = $(this); var originalTop = $this.offset().top; $(window).on('scroll', function() { var scrollTop = $(window).scrollTop(); if (scrollTop >= originalTop - settings.topSpacing) { $this.css({ position: 'fixed', top: settings.topSpacing + 'px' }); } else { $this.css({ position: '', top: '' }); } }); // Handle resize events to recalculate the offset. $(window).resize(function(){ originalTop = $this.offset().top; }); }); }; })(jQuery); // 使用插件 $('.sticky-element').sticky({ topSpacing: 20 }); ``` 另一种方式是在 HTML 结构上做文章,在 IE 下通过特定类名应用不同的样式规则来达到近似效果。例如: ```html <div class="container"> <header> <!-- 这里放置需要固定定位的内容 --> </header> </div> <style> .container header { /* 默认情况下使用标准 Sticky */ position: -webkit-sticky; position: -moz-sticky; position: -ms-sticky; position: -o-sticky; position: sticky; top: 0; } /* 针对 IE 特定版本添加额外处理 */ @media all and (-ms-high-contrast:none),(-ms-high-contrast:active){ .ie-specific-class{ position:relative !important; z-index:1000; } body.ie .container header { position: fixed; width: 100%; margin-top: 0; } } </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script> <!-- 添加条件注释用于加载针对旧版浏览器的脚本文件 --> <!--[if lt IE 9]> <script type="text/javascript" src="/path/to/polyfills-for-ie.js"></script> <![endif]--> ``` 对于更复杂的场景,还可以考虑第三方库如 [Stickybits](https://github.com/dollarshaveclub/stickybits),它提供了更好的跨平台支持和性能优化[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值