The semantic tags and attributes of HTML5 allow developers to easily achieve a clear web page layout. With CSS3 effect rendering, it is very simple to quickly build rich and flexible web pages.
The new tag elements of HTML5 are:
- <header> defines the header of the page or section;
- <footer> defines the footer of the page or section;
- <nav> defines the navigation area of the page or section;
- <section> The logical area or content combination of the page;
- <article> defines the text or a complete content;
- <aside> defines supplemental or related content;
Using them can make code semantics more intuitive and more convenient for SEO optimization. However, this new HTML5 tag cannot be recognized on IE6/IE7/IE8 and needs to be processed by JavaScript. Several methods are described below.
Method 1: Coding JavaScript
<!--[if lt IE9]> <script> (function() { if (! /*@cc_on!@*/ 0) return; var e = "abbr, article, aside, audio, canvas, datalist, details, dialog, eventsource, figure, footer, header, hgroup, mark, menu, meter, nav, output, progress, section, time, video".split(', '); var i= e.length; while (i--){ document.createElement(e[i]) } })() </script> <![endif]-->
If IE browsers below IE9 will create HTML5 tags, non-IE browsers will ignore this code, and there will be no unnecessary http requests.
Second method: use Google's html5shiv package (recommended)
<!--[if lt IE9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
但是不管使用以上哪种方法,都要初始化新标签的CSS.因为HTML5在默认情况下表现为内联元素,对这些元素进行布局我们需要利用CSS手工把它们转为块状元素方便布局
/*html5*/ article,aside,dialog,footer,header,section,footer,nav,figure,menu{display:block}
但是如果ie6/7/8 禁用脚本的用户,那么就变成了无样式的"白板"网页,我们该怎么解决呢?
我们可以参照facebook的做法,即引导用户进入带有noscript标识的 “/?_fb_noscript=1”页面,用 html4 标签替换 html5 标签,这要比为了保持兼容性而写大量 hack 的做法更轻便一些。
<!--[if lte IE 8]> <noscript> <style>.html5-wrappers{display:none!important;}</style> <div class="ie-noscript-warning">Your browser has scripts disabled, please <a href="">check here</a> to enable scripts! Or <a href="/?noscript=1" >Continue to visit</a>. </div> </noscript> <![endif]-->
This can guide the user to open the script, or jump directly to the interface designed by HTML4 tags.