简体   繁体   中英

“'HTMLDocument' is undefined” in IE8

I've been looking around, and everywhere it is said that IE8 do have HTMLDocument and Element classes. Unfortunately when I'm trying to access it, all I get is:

"'HTMLDocument' is undefined"
"'HTMLElement' is undefined"
"'Element' is undefined"
"'Document' is undefined"

I've been suspecting that it's consoles fault, but same thing happens when I load it from the script and prototype for ie Array does exist normally.

Could it be that I'm trying access it in the wrong way? I'm using:

typeof HTMLDocument.prototype.getElementsByClassName != 'function'

I am not sure, but may be, that's because you're in compatibility mode.

You can make something like:

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

But this will work for IE7+ .

Ok, I found it. IE sets itself to Quirks mode for the page I've been testing it on. Unfortunately I can't set document mode in this case, so adding my own getElementsByClassName is a lost cause...

For those that can edit html document they are working on, add this meta tag to assure IE8 will act as IE8:

<meta http-equiv="X-UA-Compatible" content="IE=8">
var hasClass = function (el, searchClass) {
    return el.className.test(new RegExp('(\\s|^)' + searchClass + '(\\s|$)'));
};

You can't add methods to HTMLElement.prototype in older versions of IE.

You can always add this to the Object.prototype object but it's frowned on

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM