在旧有的浏览器里面,很多元素都是不支持的,即使解析出来也是内联标签。所以某位外国大牛就写了JS把文本中的一些标记替换成了块标签,从而解决了IE的很多历史遗留问题。这对于HTML5的使用推广来说是一个历史性的变革,也能很好的解决一些兼容性问题。
https://github.com/sebastianbergmann/php-code-coverage/pull/128
JS代码:
002 |
* @preserve HTML5 Shiv v3.6.2pre | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed |
004 |
;( function (window, document) { |
005 |
/*jshint evil:true */ |
007 |
var version = '3.6.2pre' ; |
009 |
/** Preset options */ |
010 |
var options = window.html5 || {}; |
012 |
/** Used to skip problem elements */ |
013 |
var reSkip = /^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i; |
015 |
/** Not all elements can be cloned in IE **/ |
016 |
var saveClones = /^(?:a|b|code|div|fieldset|h1|h2|h3|h4|h5|h6|i|label|li|ol|p|q|span|strong|style|table|tbody|td|th|tr|ul)$/i; |
018 |
/** Detect whether the browser supports default html5 styles */ |
019 |
var supportsHtml5Styles; |
021 |
/** Name of the expando, to work with multiple documents or to re-shiv one document */ |
022 |
var expando = '_html5shiv' ; |
024 |
/** The id for the the documents expando */ |
027 |
/** Cached data for each document */ |
028 |
var expandoData = {}; |
030 |
/** Detect whether the browser supports unknown elements */ |
031 |
var supportsUnknownElements; |
035 |
var a = document.createElement( 'a' ); |
036 |
a.innerHTML = '<xyz></xyz>' ; |
037 |
//if the hidden property is implemented we can assume, that the browser supports basic HTML5 Styles |
038 |
supportsHtml5Styles = ( 'hidden' in a); |
040 |
supportsUnknownElements = a.childNodes.length == 1 || ( function () { |
041 |
// assign a false positive if unable to shiv |
042 |
(document.createElement)( 'a' ); |
043 |
var frag = document.createDocumentFragment(); |
045 |
typeof frag.cloneNode == 'undefined' || |
046 |
typeof frag.createDocumentFragment == 'undefined' || |
047 |
typeof frag.createElement == 'undefined' |
051 |
supportsHtml5Styles = true ; |
052 |
supportsUnknownElements = true ; |
057 |
/*--------------------------------------------------------------------------*/ |
060 |
* Creates a style sheet with the given CSS text and adds it to the document. |
062 |
* @param {Document} ownerDocument The document. |
063 |
* @param {String} cssText The CSS text. |
064 |
* @returns {StyleSheet} The style element. |
066 |
function addStyleSheet(ownerDocument, cssText) { |
067 |
var p = ownerDocument.createElement( 'p' ), |
068 |
parent = ownerDocument.getElementsByTagName( 'head' )[0] || ownerDocument.documentElement; |
070 |
p.innerHTML = 'x<style>' + cssText + '</style>' ; |
071 |
return parent.insertBefore(p.lastChild, parent.firstChild); |
075 |
* Returns the value of `html5.elements` as an array. |
077 |
* @returns {Array} An array of shived element node names. |
079 |
function getElements() { |
080 |
var elements = html5.elements; |
081 |
return typeof elements == 'string' ? elements.split( ' ' ) : elements; |
085 |
* Returns the data associated to the given document |
087 |
* @param {Document} ownerDocument The document. |
088 |
* @returns {Object} An object of data. |
090 |
function getExpandoData(ownerDocument) { |
091 |
var data = expandoData[ownerDocument[expando]]; |
095 |
ownerDocument[expando] = expanID; |
096 |
expandoData[expanID] = data; |
102 |
* returns a shived element for the given nodeName and document |
104 |
* @param {String} nodeName name of the element |
105 |
* @param {Document} ownerDocument The context document. |
106 |
* @returns {Object} The shived element. |
108 |
function createElement(nodeName, ownerDocument, data){ |
109 |
if (!ownerDocument) { |
110 |
ownerDocument = document; |
112 |
if (supportsUnknownElements){ |
113 |
return ownerDocument.createElement(nodeName); |
116 |
data = getExpandoData(ownerDocument); |
120 |
if (data.cache[nodeName]) { |
121 |
node = data.cache[nodeName].cloneNode(); |
122 |
} else if (saveClones.test(nodeName)) { |
123 |
node = (data.cache[nodeName] = data.createElem(nodeName)).cloneNode(); |
125 |
node = data.createElem(nodeName); |
128 |
// Avoid adding some elements to fragments in IE < 9 because |
129 |
// * Attributes like `name` or `type` cannot be set/changed once an element |
130 |
// is inserted into a document/fragment |
131 |
// * Link elements with `src` attributes that are inaccessible, as with |
132 |
// a 403 response, will cause the tab/window to crash |
133 |
// * Script elements appended to fragments will execute when their `src` |
134 |
// or `text` property is set |
135 |
return node.canHaveChildren && !reSkip.test(nodeName) ? data.frag.appendChild(node) : node; |
139 |
* returns a shived DocumentFragment for the given document |
141 |
* @param {Document} ownerDocument The context document. |
142 |
* @returns {Object} The shived DocumentFragment. |
144 |
function createDocumentFragment(ownerDocument, data){ |
145 |
if (!ownerDocument) { |
146 |
ownerDocument = document; |
148 |
if (supportsUnknownElements){ |
149 |
return ownerDocument.createDocumentFragment(); |
151 |
data = data || getExpandoData(ownerDocument); |
152 |
var clone = data.frag.cloneNode(), |
154 |
elems = getElements(), |
157 |
clone.createElement(elems[i]); |
163 |
* Shivs the `createElement` and `createDocumentFragment` methods of the document. |
165 |
* @param {Document|DocumentFragment} ownerDocument The document. |
166 |
* @param {Object} data of the document. |
168 |
function shivMethods(ownerDocument, data) { |
171 |
data.createElem = ownerDocument.createElement; |
172 |
data.createFrag = ownerDocument.createDocumentFragment; |
173 |
data.frag = data.createFrag(); |
177 |
ownerDocument.createElement = function (nodeName) { |
179 |
if (!html5.shivMethods) { |
180 |
return data.createElem(nodeName); |
182 |
return createElement(nodeName, ownerDocument, data); |
185 |
ownerDocument.createDocumentFragment = Function( 'h,f' , 'return function(){' + |
186 |
'var n=f.cloneNode(),c=n.createElement;' + |
188 |
// unroll the `createElement` calls |
189 |
getElements().join().replace(/\w+/g, function (nodeName) { |
190 |
data.createElem(nodeName); |
191 |
data.frag.createElement(nodeName); |
192 |
return 'c("' + nodeName + '")' ; |
198 |
/*--------------------------------------------------------------------------*/ |
201 |
* Shivs the given document. |
203 |
* @param {Document} ownerDocument The document to shiv. |
204 |
* @returns {Document} The shived document. |
206 |
function shivDocument(ownerDocument) { |
207 |
if (!ownerDocument) { |
208 |
ownerDocument = document; |
210 |
var data = getExpandoData(ownerDocument); |
212 |
if (html5.shivCSS && !supportsHtml5Styles && !data.hasCSS) { |
213 |
data.hasCSS = !!addStyleSheet(ownerDocument, |
214 |
// corrects block display not defined in IE6/7/8/9 |
215 |
'article,aside,figcaption,figure,footer,header,hgroup,nav,section{display:block}' + |
216 |
// adds styling not present in IE6/7/8/9 |
217 |
'mark{background:#FF0;color:#000}' |
220 |
if (!supportsUnknownElements) { |
221 |
shivMethods(ownerDocument, data); |
223 |
return ownerDocument; |
226 |
/*--------------------------------------------------------------------------*/ |
229 |
* The `html5` object is exposed so that more elements can be shived and |
230 |
* existing shiving can be detected on iframes. |
234 |
* // options can be changed before the script is included |
235 |
* html5 = { 'elements': 'mark section', 'shivCSS': false, 'shivMethods': false }; |
240 |
* An array or space separated string of node names of the elements to shiv. |
244 |
'elements' : options.elements || 'abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video' , |
247 |
* current version of html5shiv |
252 |
* A flag to indicate that the HTML5 style sheet should be inserted. |
256 |
'shivCSS' : (options.shivCSS !== false ), |
259 |
* Is equal to true if a browser supports creating unknown/HTML5 elements |
263 |
'supportsUnknownElements' : supportsUnknownElements, |
266 |
* A flag to indicate that the document's `createElement` and `createDocumentFragment` |
267 |
* methods should be overwritten. |
271 |
'shivMethods' : (options.shivMethods !== false ), |
274 |
* A string to describe the type of `html5` object ("default" or "default print"). |
280 |
// shivs the document according to the specified `html5` object options |
281 |
'shivDocument' : shivDocument, |
283 |
//creates a shived element |
284 |
createElement: createElement, |
286 |
//creates a shived documentFragment |
287 |
createDocumentFragment: createDocumentFragment |
290 |
/*--------------------------------------------------------------------------*/ |
293 |
window.html5 = html5; |
296 |
shivDocument(document); |
然后把html5shiv.js在head里面引入:
04 |
< meta charset = "UTF-8" > |
06 |
< script type = "text/javascript" src = "html5.js" ></ script > |
07 |
< style type = "text/css" > |
16 |
< nav >Springload</ nav >Springload |
在IE8中打开查看你就会发现nav这个HTML5元素标签,可以正常使用和显示。