JavaScript常用语句总结!

来源:互联网 发布:js urlencode 编辑:程序博客网 时间:2024/06/16 10:45

JS判空

function checkData(StackEmpty){
if(StackEmpty == null || StackEmpty=="null" || typeof(StackEmpty)=="undefined" || StackEmpty=="undefined" || StackEmpty=="" ){
 return false ;//为null,返回false
}
if(!isNaN(StackEmpty) && StackEmpty!="null" && typeof(StackEmpty)!="undefined" && StackEmpty!="undefined"&& StackEmpty!="" ){
 return true ;//不为null,返回true
}
}


JS定时循环

t=setTimeout('getAlarm()',100000);
//调用本身实现方法循环
var setIntervalValue = setInterval('energyallTable()',5000);
//定时每多少秒执行函数


JS判断浏览器

if ((navigator.userAgent.indexOf('MSIE') >= 0)&& (navigator.userAgent.indexOf('Opera') < 0)) {
} else if (navigator.userAgent.indexOf('Firefox') >= 0) {
} else {
}


JS对时间格式的处理
//Date转换成时间戳
var dateLong = date.getTime();
//时间戳转换成Date
var dateDate = new Date(dateLong);
//String 转dare
var  str  =  "2010-03-22"; 
 var val = Date.parse(str);
var newDate = new Date(val);;


JS四舍五入
//num是原数,,dec是哪一位后全设为0 ,四舍五入到哪位
 function round(num, dec) {
var sNum = num + '';
var idx = sNum.indexOf(".");
if (idx < 0)
return num;
var n = sNum.length - idx - 1;
if (dec < n) {
var e = Math.pow(10, dec);
return Math.round(num * e) / e;
} else {
return num;
}
}
或者是s 四舍五入函数 toFixed(),里面的参数 就是保留小数的位数。
0 0
原创粉丝点击