假设我们有一个文档(在IE8中),那么通常我们可以假定document.childNodes[0]
是文档类型。因此
var doctype = document.childNodes[0]
现在,我们如何确认它确实是文档类型,而不是仅仅假设呢?
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
除了假设,我怎么知道给定的节点是否是文档类型?
对于不熟悉DOM4的人,请参阅
DocumentType
DOM-shim在IE8中通过返回document.childNodes [0]
来获取document.doctype
。
Node.DOCUMENT_TYPE_NODE
。 - millimoosedocument.childNodes[0]
是什么。 - millimoosedocument.doctype
。 - Raynosdocument.childNodes[0]
处有注释。这是一个悲伤的日子。IE8又做错了一件事 :( - Raynos