Javascript ';HTMLElement';IE8中是否有未定义的选项?

Javascript ';HTMLElement';IE8中是否有未定义的选项?,javascript,internet-explorer-8,Javascript,Internet Explorer 8,嘿,我所有的方法都是这样的: // Has Class HTMLElement.prototype.hasClass = function (searchClass) { return this.className.match(new RegExp('(\\s|^)' + searchClass + '(\\s|$)')); } 在IE9中,它运行良好。在IE8中,它给了我未定义的。。。有简单的解决方法吗?如果我没记错的话,你不能在旧版本的IE中向HTMLElement.prototy

嘿,我所有的方法都是这样的:

// Has Class
HTMLElement.prototype.hasClass = function (searchClass) {
    return this.className.match(new RegExp('(\\s|^)' + searchClass + '(\\s|$)'));
}

在IE9中,它运行良好。在IE8中,它给了我未定义的。。。有简单的解决方法吗?

如果我没记错的话,你不能在旧版本的IE中向
HTMLElement.prototype
添加方法。一个简单的解决办法是:

var hasClass = function (el, searchClass) {
    return el.className.test(new RegExp('(\\s|^)' + searchClass + '(\\s|$)'));
};
用起来像:

alert(    hasClass(   document.getElementById('div1'), 'classToCheck'   )    )
您始终可以将其添加到
对象。prototype
对象中,但它不受欢迎