我刚看到了html5shiv并找到了这段代码:
function addStyleSheet(ownerDocument, cssText) {
var p = ownerDocument.createElement('p'),
parent = ownerDocument.getElementsByTagName('head')[0] || ownerDocument.documentElement;
p.innerHTML = 'x<style>' + cssText + '</style>';
return parent.insertBefore(p.lastChild, parent.firstChild);
}
在哪里使用 p.innerHTML = 'x<style>' + cssText + '</style>';
。为什么这里使用x?
X-UA-Compatible
有关? - Mike Antep
并没有被插入到parent
中,而是插入了p.lastChild
(即仅插入了<style>
标签,而不是x
文本)。这似乎是为了处理某些奇怪的浏览器问题,其中innerHTML
不能仅使用一个<style>
元素,而需要在其之前添加一些其他文本。 - apsillers