Ray*_*nos 8 html doctype dom internet-explorer-8
所以,让我们说(在IE8中)我们有一个document.
现在通常我们可以假设这document.childNodes[0]是doctype.所以
var doctype = document.childNodes[0]
现在我们如何确认而不是假设它是doctype?
doctype.nodeType === Node.COMMENT_NODE;
doctype.tagName === "!"; // same as a comment
doctype.data.indexOf("DOCTYPE ") > -1; // same as any comment containing the word DOCTYPE.
doctype === document.doctype; // false, document.doctype is undefined in IE8
Run Code Online (Sandbox Code Playgroud)
除了假设,我怎么知道给定节点是否是doctype?
对于那些不熟悉DOM4的人来看看 DocumentType
只需返回,DOM-shim就可以document.doctype在IE8中获取document.childNodes[0]
我发现在 IE7 和 IE8 中,.innerHTML注释属性始终是 full "<!--comment here-->",对于 doctype 节点,它是"". 即使对于空注释,.innerHTML也是"<!---->"
所以基于此我创建了:
function isDocType(node) {
return node && (
node.nodeType === 10 || (node.nodeType === 8 && "innerHTML" in node && node.innerHTML === ""));
}
Run Code Online (Sandbox Code Playgroud)
与测试页:
<!-- comment1 -->
<!-- comment2 -->
<!---->
<!-- -->
<!-- <!DOCTYPE html> -->
Run Code Online (Sandbox Code Playgroud)
运行 doctype 和注释isDocType给出:
LOG: true
LOG: false
LOG: false
LOG: false
LOG: false
LOG: false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
398 次 |
| 最近记录: |