关于html:在IE中显示DIV而不是Section/Article

Display DIV instead of Section / Article in IE

我基本上想创建一个支持 HTML5 语义元素的网页,但是对于不注意 HTML5 元素的浏览器,我该怎么办?

例如,我想使用现代浏览器注意到的 <section></section>,但是如果有人在旧浏览器中访问我的网站,我该怎么做才能显示 DIV 而不是 SECTION?

我尝试使用以下代码,它只对 Internet Explorer 有用,但对其他浏览器有用吗?

1
2
3
4
5
<section class="main">
  <!--[if IE]>
   Only displayed only in IE .
  <![endif]-->
</section>

有什么建议吗?


HTML5 Shiv 支持在旧版 Internet Explorer 中使用 HTML5 分段元素,并为 Internet Explorer 6-9、Safari 4.x(和 iPhone 3.x)和 Firefox 3.x 提供基本的 HTML5 样式。

1
2
3
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
<![endif]-->


你可以在 IE 中加入这个小脚本:

1
(function(){if(!/*@cc_on!@*/0)return;var e ="abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','),i=e.length;while(i--){document.createElement(e[i])}})()

如果你使用 document.createElement.

,IE 可以理解新的和自定义的元素

参考:http://css-tricks.com/snippets/javascript/make-html5-elements-work-in-old-ie/


解决这个问题的好方法是包含 JavaScript polyfill 库 Modernizr。它使 HTML5 元素在旧版本的 IE 中工作。这样你现在就可以开始使用 HTML5 语义标签,而不是等到旧的 IE 死掉 ;)


把这段代码放在你的head标签里IE浏览器支持HTML5标签,

1
2
3
4
 <!--[if IE]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
        <script src="http://www.toyotapriusprojects.com/wp-content/themes/html5/lib/js/innershiv.min.js">
        <![endif]-->