前端常用的工具类

时间:2020-06-30
本文章向大家介绍 前端常用的工具类 ,主要包括 前端常用的工具类 使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
/* 动态生成A-Z */
function getEN(){
var arr = [];  //定义数组
for(var i = 65; i < 91; i++){
arr.push(String.fromCharCode(i));  //ASKLL值对应的字符
}
return arr.join(',');  //输出A,B,C...
}
/* 转换时间格式 */
var csrq = convert_time(466646400000);  //后台传过来的时间戳转换时间格式
function convert_time (date) {
if(date == "" || date == null || date == undefined){
date = "";
return date;
}else{
date = new Date(date);
var year =date.getFullYear();//获取完整的年份(4位,1970-????)
var month = date.getMonth() + 1;//获取当前月份(0-11,0代表1月)
var day = date.getDate();//获取当前日(1-31)
var hours = date.getHours();
var min = date.getMinutes();
if (month < 10) {
month ="0" + month;
}
if (day < 10) {
day ="0" + day;
}
var dateString = year +"/" + month + "/" + day ;
return dateString;
}
}
/* 计算两个时间段差 */
function get_time_diff(time) {
var diff = '';
var time_diff = new Date().getTime() - time; //时间差的毫秒数
//计算出相差天数
var days = Math.floor(time_diff / (24 * 3600 * 1000));
if (days > 0) {
diff += days + '天';
}
//计算出小时数
var leave1 = time_diff % ( 24 * 3600 * 1000);
var hours = Math.floor(leave1 / (3600 * 1000));
if (hours > 0) {
diff += hours + '小时';
} else {
if (diff !== '') {
diff += hours + '小时';
}
}
//计算相差分钟数
var leave2 =leave1 % (3600 * 1000);
var minutes = Math.floor(leave2 / (60 * 1000));
if (minutes > 0) {
diff += minutes + '分';
} else {
if (diff !== '') {
diff += minutes + '分';
}
}
//计算相差秒数
var leave3 = leave2%(60*1000);
var seconds = Math.round(leave3/1000);
if (seconds > 0) {
diff += seconds + '秒';
} else {
if (diff !== '') {
diff += seconds + '秒';
}
}
return diff;
}
/* 判断对象是否为空 等于空为true */
function isKong(object) {
return !(object !== undefined && object !== "" && object != null && object !== "null" && object !== "-1" && object !== -1 && object !=='undefined');
}
/* 返回内容 */
function kong(obj) {
//typeof 返回的是字符串,有六种可能:"number"、"string"、"boolean"、"object"、"function"、"undefined"
if (typeof(obj) == "undefined" || obj == "" || obj == null || obj == "null" || obj.length == 0) {
return "";
} else {
//删除全是空格的情况
var regu = "^[ ]+$";
var re = new RegExp(regu);
if (re.test(obj)) {
return "";
} else {
return obj;
}

}
}
/* 内容过长展示指定字数 */
function formatStr(param,len) {  //param:内容,len:长度
var res ="";
if(param.length>len){
res = param.substring(0,len)+"......";
}else {
res = param;
}
return res;
}
 /* 加载页面时触发,获取查过来的参数 */
跳转前页面:window.location.href="./exam/test.html?ksdh="+ksdh+"&qssj="+ time
      var url = location.search.split('-').join(' ');
var theRequest = new Object();
if(url.indexOf("?") != -1){
var str = url.substr(1);
var strs = str.split("&");
for(var i = 0; i < strs.length; i++){
theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
    }
var ksdh = parseInt(theRequest.ksdh); //试卷单号
var qssj = theRequest.qssj.split('%').join(' '); //进入试卷时的时间

   }
/* input框数字校验 */
<input type = "number" oninput="judgeScore(this)">
function judgeScore(obj){
//先把非数字的都替换掉,除了数字和.
obj.value = obj.value.replace(/[^\d.]/g,"");
//保证只有出现一个.而没有多个.
obj.value = obj.value.replace(/\.{2,}/g,".");
//必须保证第一个为数字而不是.
obj.value = obj.value.replace(/^\./g,"");
//保证.只出现一次,而不能出现两次以上
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
//只能输入一个小数(几个小数就是几个\d
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3');
      //最大值为99.9
if(obj.value > 99.9){
obj.value = 99.9;
      }
      //将"01","02"...替换为"1","2"...
      for(var i = 0; i < 10; i ++){
       var num = "0" + i;
      if(obj.value == num){
      obj.value = i;
      }
      }

}

						

$flag 上一页 下一页