本文主要包含ie 6,json,兼容等相关知识,一个无聊的程序员的琐碎希望在学习及工作中可以帮助到您
1、使用jqeury的JSON方法
$.parseJson(json)
2、eval方法
var jsons = req.responseText;
var s;
if (typeof(JSON) == 'undefined'){
s = eval("("+jsons+")");
}else{
s = JSON.parse(jsons);
}
此方法有个小问题,就是会执行里面的js代码
3、引用json.js
<script type="text/JavaScript" src="js/json2.js"></script>
json.js
IE6、IE7兼容querySelectorAll和querySelector方法
if (!document.querySelectorAll) {
document.querySelectorAll = function (selectors) {
var style = document.createElement('style'), elements = [], element;
document.documentElement.firstChild.appendChild(style);
document._qsa = [];
style.styleSheet.cssText = selectors + '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
window.scrollBy(0, 0);
style.parentNode.removeChild(style);
while (document._qsa.length) {
element = document._qsa.shift();
element.style.removeAttribute('x-qsa');
elements.push(element);
}
document._qsa = null;
return elements;
};
}
if (!document.querySelector) {
document.querySelector = function (selectors) {
var elements = document.querySelectorAll(selectors);
return (elements.length) ? elements[0] : null;
};
}
当querySelector和querySelectorAll在ie6 7 下未定义的情况,直接重新定义querySelector和querySelectorAll。
兼容ie8 下拉框val()方法
在ie8以上$(“XXX”).val()方法获取的是选中的value,而当ie8及以下的时候获取的是一个数组,$(“XXX”).val()[0]才能获取正确的值。
var clsCodeValue = "";
clsCodeValue = $("#clsCode").val();
clsCodeValue = typeof clsCodeValue == "object" ? clsCodeValue[0] : clsCodeValue;
clsCodeValue = $.trim(clsCodeValue);
如果clsCodeValue是数组的话,clsCodeValue的值为clsCodeValue[0]
如果clsCodeValue不是数组的话,clsCodeValue不变