JAvascriptの一般的な操作の概要

11433 ワード

文字列、時間、フォーム、正規検証などjavascriptでよく見られるさまざまな操作をまとめました.極めて高い参考価値を持っている.皆さんの参考にしてください.具体的な方法は以下の通りです.

/***** BasePage.js                 jquery  *****/
 
//#region     
 
//        。
function stringtoTime(date1) {
  var dt = new Date(Date.parse(date1.replace(/-/g, "/")));
  return dt;
}
 
//    var date1 = "2013-06-08 15:23:31" "2013/6/8 9:9:00"  ;
//-------------------------------------------------------------
//      
Date.prototype.format = function (format) {
  var o = {
    "M+": this.getMonth() + 1, //month 
    "d+": this.getDate(), //day 
    "h+": this.getHours(), //hour 
    "m+": this.getMinutes(), //minute 
    "s+": this.getSeconds(), //second 
    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
    "S": this.getMilliseconds() //millisecond 
  }
 
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
 
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return format;
}
 
////     
 
//alert(newdate.format("yyyy-MM-dd"));
//alert(newdate.format("MM/dd/yyyy"));
//var nowStr = now.format("yyyy-MM-dd hh:mm:ss"); Math.round(Math.random() * 10000)
////    2: 
//var testDate = new Date();
//var testStr = testDate.format("yyyy MM dd hh  mm ss ");
////  : 
//alert(testDate.format("yyyy MM dd "));
//-------------------------------------------------------------
//        (  )
function SetFlag(start, end) {
  var cdate = Array();
  cdate = start.split("-");
  var cd = cdate[1] + "/" + cdate[2] + "/" + cdate[0];
  var dayNum = DateDiff(end, start);
  for (var i = 0; i <= dayNum; i++) {
    flag.push(AddDays(cd, i));
  }
} //end fun
 
//           .
function AddDays(date, days) {
  var nd = new Date(date);
  nd = nd.valueOf();
  nd = nd + days * 24 * 60 * 60 * 1000;
  nd = new Date(nd);
  //alert(nd.getFullYear() + " " + (nd.getMonth() + 1) + " " + nd.getDate() + " ");
  var y = nd.getFullYear();
  var m = nd.getMonth() + 1;
  var d = nd.getDate();
  if (m <= 9) m = "0" + m;
  if (d <= 9) d = "0" + d;
  var cdate = y + "-" + m + "-" + d;
  return cdate;
}

//       (d1 - d2).
function DateDiff(d1, d2) {
  var day = 24 * 60 * 60 * 1000;
  try {
    var dateArr = d1.split("-");
    var checkDate = new Date();
    checkDate.setFullYear(dateArr[0], dateArr[1] - 1, dateArr[2]);
    var checkTime = checkDate.getTime();
 
    var dateArr2 = d2.split("-");
    var checkDate2 = new Date();
    checkDate2.setFullYear(dateArr2[0], dateArr2[1] - 1, dateArr2[2]);
    var checkTime2 = checkDate2.getTime();
 
    var cha = (checkTime - checkTime2) / day;
    return cha;
  } catch (e) {
    return false;
  }
} //end fun
 
//#endregion
 
//#region URL  
 
/*
*   QueryString       
*/
function getQueryStringByName(name) {
  var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
  if (result == null || result.length < 1)
    return "";
 
  return result[1];
}
 
function GetQueryString(name) {
  //         ,                ,     
  if (location.href.indexOf("?") == -1 || location.href.indexOf(name + '=') == -1) {
    return '';
  }
 
  //          
  var queryString = location.href.substring(location.href.indexOf("?") + 1);
 
  //       ?key=value&key2=value2
  var parameters = queryString.split("&");
 
  var pos, paraName, paraValue;
  for (var i = 0; i < parameters.length; i++) {
    //       
    pos = parameters[i].indexOf('=');
    if (pos == -1) {
      continue;
    }
    //   name   value
    paraName = parameters[i].substring(0, pos);
    paraValue = parameters[i].substring(pos + 1);
    //      name    name,      ,  ,     +      
    if (paraName == name) {
      return unescape(paraValue.replace(/\+/g, " "));
    }
  }
  return '';
}
 
/*
*             
*/
function getCurrentUrlName() {
  var url = window.location.href;
  url = url.toLocaleLowerCase();
  if (url == undefined || url == "")
    return "";
  var item = url.toString().split("/");
  var name = item[item.length - 1];
  name = name.toString().split(".");
  if (name.length == 2)
    return name[0].toString();
  else return "";
}
 
//#endregion
 
//#region      
 
/*
*          
*   :
*   strString:         
*   strStart:      
*   intLen:       
*/
function getCustomLengtStr(strString, strStart, intLen) {
  if (strString != undefined && strString != "" && strString != null) {
    var CLen = strString.toString().length;
    if ((strStart + intLen) <= CLen - 1) {
      if (strStart < 0) strStart = 0;
 
      return strString.toString().substr(strStart, Number(intLen));
    } else {
      //    ,      
      return strString;
    }
  } else return "";
}
 
/*
*             
*   :
*   strString:         
*   intStart:      
*   intEnd:       
*/
function getCustomLengtStr(strString, intStart, intEnd) {
  if (strString != undefined && strString != "" && strString != null) {
    var Clen = strString.toString().length - 1;
    if (Number(intEnd) <= Clen) {
      if (intStart < 0) intStart = 0;
      else if (intStart > Clen) intStart = Clen;
 
      return strString.toString().substring(intStart, intEnd);
    } else {
      //    ,      
      return strString;
    }
  } else return "";
}
 
//#endregion
 
//#region     ,    
 
//    
function SetHome() {
  if (document.all) {
    document.body.style.behavior = "url(#default#homepage)";
    var url = window.location.href;
    document.body.setHomePage(url);
  } else {
    alert("      ,     !");
  }
}
 
//     
function AddCollect() {
  var url = window.location.href;
  try {
    window.external.addFavorite(url, "    ");
  }
  catch (e) {
    try {
      window.sidebar.addPanel("    ", url, "");
    }
    catch (e) {
      alert("      ,   Ctrl+D    ");
    }
  }
}
 
//#endregion 
 
//#region   /   
 
//CheckSelectAll(true);
 
function CheckSelectAll(check) {
  $("input[type='checkbox']").attr("checked", check)
}
 
//#endregion
 
//#region       
 
//CountDown("#btnGetCode",60);
 
function CountDown(item, times) {//      ,  (s)
  var timer = setInterval(function () {
    var btnValidate = $(item);
    if (times > 0) {
      btnValidate.attr("disabled", "false").css("opacity", "0.5").val("    (" + times + ")");
      times--;
    } else {
      btnValidate.removeAttr("disabled").css("opacity", "1").val("     ");
      clearInterval(timer);
    }
  }, 1000);
}
 
//#endregion
 
//#region         
 
//ClearEmpty("#txtName");
 
function ClearEmpty(obj) {//      
  $(obj).focus(function () {
    if ($(this).val() == this.defaultValue) {
      $(this).val("");
    }
  }).blur(function () {
    if ($(this).val() == "") {
      $(this).val(this.defaultValue);
    }
  });
}
 
//#endregion
 
//#region     
 
function Refresh() {
  window.parent.location.reload();
}
 
//#endregion
 
//#region     
 
//#region        
 
// var item = checkChinese("  ");
 
function checkChinese(obj) {
  var reg = /[^\u4e00-\u9fa5]/;
  return !reg.test(obj);
}
 
//#endregion
 
//#region        
 
// var item = checkNum("123");
 
function checkNum(obj) {
  var reg = /^\d+$/;
  return reg.test(obj);
}
 
//#endregion
 
//#region        
 
// var item = checkLetter("abc");
 
function checkLetter(obj) {
  var reg = /^[a-zA-Z]+$/;
  return reg.test(obj);
}
 
//#endregion
 
//#region           
 
// var item = checkLetterNum("abc123");
 
function checkLetterNum(obj) {
  var reg = /^[a-zA-Z0-9]+$/;
  return reg.test(obj);
}
 
//#endregion
 
//#region           
 
// var item = checkLetterNum("abc123");
 
function checkLetterChina(obj) {
  var reg = /^[A-Za-z\u4E00-\u9FA5]+$/;
  return reg.test(obj);
}
 
//#endregion
 
//#region              
 
// var item = checkLetterNum("abc123");
 
function checkLetterChinaNum(obj) {
  var reg = /^[a-zA-Z0-9\u4E00-\u9FA5]+$/;
  return reg.test(obj);
}
 
//#endregion
 
//#region         
 
// var item = checkMobile("13888888888")
 
function checkMobile(obj) {
  var reg = /^[1][3458][0-9]{9}$/;
  return reg.test(obj);
}
 
//#endregion
 
//#region       
 
// var item = checkEmail("[email protected]")
 
function checkEmail(obj) {
  var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return reg.test(obj);
}
 
//#endregion
 
//#region        
 
// var item = checkIDCard("555555555555555555")
 
function checkIDCard(obj) {
  var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  return reg.test(obj);
}
 
//#endregion
 
//   
function GetRandomNum(Min, Max) {
  var Range = Max - Min;
  var Rand = Math.random();
  return (Min + Math.round(Rand * Range));
} 
 
//#endregion


本文で述べたjavascriptを用いたWEBプログラムの設計には参考になると信じています.