1:判断字符串和对象和数组是否为空:
var hasOwnProperty = Object.prototype.hasOwnProperty;
//判断一个数组,对象,字符串是否为空
function isEmpty(obj) {
// 本身为空直接返回true
if (obj == null) return true;
// 然后可以根据长度判断,在低版本的ie浏览器中无法这样判断。
if (obj.length > 0) return false;
if (obj.length === 0) return true;
//最后通过属性长度判断。
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}
return true;
}
2:下载文件:
window.downloadFile = function (sUrl) {
//iOS devices do not support downloading. We have to inform user about this.
if (/(iP)/g.test(navigator.userAgent)) {
alert('Your device does not support files downloading. Please try again in desktop browser.');
return false;
}
//If in Chrome or Safari - download via virtual link click
if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
//Creating new link node.
var link = document.createElement('a');
link.href = sUrl;
if (link.download !== undefined) {
//Set HTML5 download attribute. This will prevent file from opening if supported.
var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
link.download = fileName;
}
//Dispatching click event.
if (document.createEvent) {
var e = document.createEvent('MouseEvents');
e.initEvent('click', true, true);
link.dispatchEvent(e);
return true;
}
}
// Force file download (whether supported by server).
if (sUrl.indexOf('?') === -1) {
sUrl += '?download';
}
window.open(sUrl, '_self');
return true;
}
window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
调用方法:window.downloadFile(url)
3:返回url对象
//返回url参数对象
function zs_getUrlParamObj() {
var name, value, str, num, arr;
var retObj = {};
str = location.href.substr(location.href.indexOf("?") + 1);
arr = str.split("&"); //各个参数放到数组里
for (var i = 0; i < arr.length; i++) {
num = arr[i].indexOf("=");
if (num > 0) {
name = arr[i].substring(0, num);
value = arr[i].substr(num + 1);
retObj[name] = value;
}
}
return retObj;
}
用法:eg: "a.html"+"?deviceId="+deviceid + "&loopName=" + loopName;
页面中用 var urlCanshu = zs_getUrlParamObj();
urlCanshu.deviceId
urlCanshu.loopName
4:判断数组是否重复
function arrDouble(a, b) {
/*a是一个数组
b:分割字符;
*/
var isT = false;
for (var len = 0; len < a.length; len++) {
if (b == a[len]) {
isT = true;
break;
}
}
;
return isT
}
用法:eg:
var gg = arrDouble(isDouble,indexName);
if(gg){
layer.msg("该指标项已经存在!", {time : 1000, icon : 2});
}
5:字符串截取
function stringSplit(a, b, c) {
/*a:分割对象; a是一个数组
b:分割字符;
c:分割后取值的下标
*/
var strs = new Array(), //定义一数组
arr = new Array(),
code = '';//分割后的字符输出
for (var len = 0; len < a.length; len++) {
strs = a[len].split(b); //字符分割
arr.push(strs[c]);
if (len == 0) {
code += arr[0];
} else {
code += ',' + arr[len];
}
;
}
;
return code;
}
用法:eg:
var str = ['ECEP_EQUIP_TYPE_VALUE_index00','ECEP_EQUIP_TYPE_VALUE_index01','ECEP_EQUIP_TYPE_VALUE_index02','ECEP_EQUIP_TYPE_VALUE_index03'];
var code1 = stringSplit(str,"ECEP_EQUIP_TYPE_VALUE_",1)
console.log(code1);
"index00,index01,index02,index03"
6:删除数组元素
//根据数组下标删除数组中某个元素
Array.prototype.del = function (index) {
if (isNaN(index) || index >= this.length) {
return false;
}
for (var i = 0, n = 0; i < this.length; i++) {
if (this[i] != this[index]) {
this[n++] = this[i];
}
}
this.length -= 1;
};
//删除数组某个元素(根据value值)
Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
Array.prototype.remove = function (val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
7:tab切换
function TabChange() {
};
TabChange.prototype = {
on: function (tabs, divs, liClaON, liclass) {
var _this = this;
if (tabs.length != divs.length) {
alert("菜单层数量和内容区数量不一致!");
return false;
}
for (var i = 0; i < tabs.length; i++) {
_this.$(tabs[i]).value = i;
_this.$(tabs[i]).onclick = function () {
for (var j = 0; j < tabs.length; j++) {
_this.$(divs[j]).style.display = "none";
_this.$(tabs[j]).className = '' + liclass;
}
_this.$(tabs[this.value]).className = '' + liClaON;
_this.$(divs[this.value]).style.display = "block";
}
}
},
$: function (oclass) {
if (typeof (oclass) == "string")
return document.getElementsByClassName(oclass);
return oclass;
}
};
// tab切换调用
var tabChange = new TabChange();
var Lists = document.getElementsByClassName('tabLi');
var cons = document.getElementsByClassName('TabCon');
tabChange.on(Lists, cons, 'tabLi tabON', 'tabLi');