IE6-8 不識別 HTML5 新元素(section、article、header 等),無法為其應用樣式、無法正常構建 DOM。經典解法是 document.createElement('tagName')「註冊」元素,但動態插入時有坑。
<!--[if lt IE 9]>
<script>
(function(tags){
tags.forEach(function(t){ document.createElement(t); });
})('article aside audio canvas details figcaption figure footer header hgroup main mark meter nav output progress section summary time video'.split(' '));
</script>
<![endif]-->
或引入 html5shiv(含 print 保護、媒體元素支援)。
var div = document.createElement('div');
div.innerHTML = '<section>section</section>';
alert(div.innerHTML); // IE6-8 顯示 "</section>",section 被剝離成無標籤文本節點
var div = document.createElement('div');
div.innerHTML = 'fixie<div>' + '<section>section</section>' + '</div>';
alert(div.innerHTML); // 正確保留 section
alert(div.lastChild.innerHTML); // 可正常訪問 section 內容
原理:在未知元素外層包一個已知元素(如 div),欺騙 IE 解析器正常構建子樹。
html5shiv + innerShiv(或 html5shiv-printshiv)完整解決動態插入問題。