在IE8中扩展Element.prototype时"Object doesn't support method"错误



我知道IE8支持Element对象的扩展。我尝试了一个简单的示例,它在Google Chrome中起作用,但在IE8中不起作用。这是我的代码,,这是一个JSFIDDLE,可以查看我的代码

html

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

javascript

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

错误

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

我在做什么错?


Update

确定,对于本地错误,IE8似乎不喜欢我的<!DOCTYPE>标签。所以我找到了另一个。= P IE8对这样的事情非常严格。如果元素没有有效的html,则IE8不会在诸如document.getElementsByTagName()之类的东西中捡起它。

mootools覆盖基线Element

查看

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

带有mootools返回false,除了mootools以外的任何其他内容,它应该返回true。

相关内容

最新更新