Javascript Custom element.prototype函数具有未定义的;这";
我正在尝试创建一个备份函数,它将在浏览器不支持类列表的情况下使用Javascript Custom element.prototype函数具有未定义的;这";,javascript,internet-explorer-8,Javascript,Internet Explorer 8,我正在尝试创建一个备份函数,它将在浏览器不支持类列表的情况下使用 if(!Element.prototype.classList) { Element.prototype.classList = {}; Element.prototype.classList.contains = function(class_name){ console.log(this) } } console.log(this)返回“undefined”,如何获取调用函数的
if(!Element.prototype.classList) {
Element.prototype.classList = {};
Element.prototype.classList.contains =
function(class_name){
console.log(this)
}
}
console.log(this)返回“undefined”,如何获取调用函数的元素?可能是IE8的
console.log()
以某种方式“破坏”了结果。在IE8兼容模式(在IE11中)的控制台中,这将正确显示[object object]{}
作为返回值,可展开以显示附加的contains()
函数:
Element.prototype.classList = {};
Element.prototype.classList.contains = function() {
return this;
};
var n = document.createElement('div');
n.classList.contains(x);
但是,将返回值包装在console.log()
中会显示undefined
。不过,只要让它“流经”控制台,就可以按预期显示对象
可能与古老的IE怪癖有关,在IE怪癖中,控制台
不存在,除非开发人员工具被打开