跳到主要内容

-- 兼容性解决方案

Create by fall on ---- Recently revised in 2021年11月11日20:29:12

兼容性解决方案

// 兼容IE8 不支持 node.getElementsByClassName
function elementsByClassName(node,classStr){
var nodes = node.getElementsByTagName("*");
var arry =[];
for (var i = 0 ;i<nodes.length;i++){
if(nodes[i].className === classStr){
arry.push(nodes[i])
}
}
return arry;
}

//解决浏览器的外联样式属性选择问题
function getStyle(node,cssStyle){
return node.currentStyle ? node.currentStyle['height'] : getComputedStyle(oDiv["height"])
}

LocalStorage

使用

localStorage.setItem('a','1')
localStorage.b = '2'
localStorage["c"] = '3'

localStorage 的兼容

function storageTest(){
const testKey = 'test_key';
const testValue = 'test_value';
let isSupport = false;
try {
localStorage.setItem(testKey, testValue);
if (localStorage.getItem(testKey) === testValue) {
isSupport = true;
}
localStorage.removeItem(testKey);
return isSupport;
} catch(e) {
if (e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
console.warn('localStorage 存储已达上限!')
} else {
console.warn('当前浏览器不支持 localStorage!');
}
return isSupport;
}
}