What's IE take on HTMLDocument and HTMLElement

2020-07-03 08:12发布

问题:

Within javascript's scope, referring to HTMLDocument or HTMLElement raises error on IE8.

The error I get is "HTMLElement is undefined".

What is the way to have JS interacting with native DOM object of this browser?

回答1:

In IE8 you have to use the Element and HTMLDocument classes. In IE7... nothing, because IE7 is terrible for standards. You have to rely on jQuery or other frameworks that wrap DOM elements.

In my own framework I make this simple check:

var elementPrototype = typeof HTMLElement !== "undefined"
        ? HTMLElement.prototype : Element.prototype;

Mind you that it's not a framework for IE7 and lower.