在IE8中,'HTMLElement'是未定义的,另一种选择? [英] 'HTMLElement' is undefined in IE8, an alternative?

查看:1256
本文介绍了在IE8中,'HTMLElement'是未定义的,另一种选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有我有这样的方法:

Hey all I have methods like this:

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

在IE9中,它运行正常。在IE8中,它给了我未定义的......有一个简单的解决方法吗?

In IE9 it works fine. In IE8 it gives me undefined... is there a simple work around?

推荐答案

你不能将方法添加到<$ c = c> HTMLElement.prototype 在旧版本的IE中,如果我没记错的话。一个简单的解决方法是:

You can't add methods to HTMLElement.prototype in older versions of IE if I remember correctly. A simple workaround would be:

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

并使用如:

alert(    hasClass(   document.getElementById('div1'), 'classToCheck'   )    )



DEMO



您随时可以添加这到 Object.prototype 对象,但它不赞成

这篇关于在IE8中,'HTMLElement'是未定义的,另一种选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆