我需要在 Jquery 中找到一些可以在 IE8 和真实浏览器中工作的东西。我是 Jquery 的新手,以下是我在现代浏览器中运行的代码:
$('#FIELD_'+office_id).on('change',function(){
offices = $(this).val();
for(var i=0; i<=Object.keys(southland.address).length;i++){
if(offices == Object.keys(southland.address)[i]){
address = southland.address[offices]Object.keys(southland.address[offices])[0]];
}
}
其中 Southland.address 来自外部数组。这在 Chrome、IE10 和 FF 中完美运行,我能为 IE8 做什么?
要在旧版浏览器中支持 Object.keys,您可以使用以下代码片段:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Compatibility
if (!Object.keys) {
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) result.push(prop);
}
if (hasDontEnumBug) {
for (var i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
}
}
return result;
};
})();
}
或这个polyfill(也包括其他垫片):
https://github.com/kriskowal/es5-shim/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)