简体   繁体   中英

“Object doesn't support method” error when extending Element.prototype in IE8

I understand that IE8 supports extension of the Element object. I tried a simple example, and it works in Google Chrome but not in IE8. Here is my code, and here is a jsfiddle to see my code :

HTML

<div id='test'>Hi</div>

JavaScript

Element.prototype.test = function(){
  alert('yup');
}

document.getElementById('test').test();

Errors

//jsfiddle.net => Object doesn't support property or method 'test'
//local test => 'Element' is undefined

What am I doing wrong?


UPDATE

Ok as for the local error, it seems that IE8 didn't like my <!DOCTYPE> tag. So I found another one. =p IE8 is very strict on things like that. If an element isn't valid HTML, IE8 won't pick it up in things such as document.getElementsByTagName() .

MooTools overrides the baseline Element .

Check out

var e = document.getElementById('test');
console.log(e instanceof Element);

with MooTools on it returns false, with anything but MooTools it should return true.

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