JavaScript常用語句總結!

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(),裏面的參數 就是保留小數的位數。
發佈了15 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章