js常用代码

https://www.oschina.net/question/3775878_2280515

【输出控制台】

console.log()

arr=[];//打印数组

for(var key in arr){
 document.writeln(arr[key]);
}

【计算日期】

var myDate = new Date();
myDate.getYear();        //获取当前年份(2位)
myDate.getFullYear();    //获取完整的年份(4位,1970-????)
myDate.getMonth();       //获取当前月份(0-11,0代表1月)
myDate.getDate();        //获取当前日(1-31)
myDate.getDay();         //获取当前星期X(0-6,0代表星期天)
myDate.getTime();        //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours();       //获取当前小时数(0-23)
myDate.getMinutes();     //获取当前分钟数(0-59)
myDate.getSeconds();     //获取当前秒数(0-59)
myDate.getMilliseconds();    //获取当前毫秒数(0-999)
myDate.toLocaleDateString();     //获取当前日期
var mytime=myDate.toLocaleTimeString();     //获取当前时间
myDate.toLocaleString( );        //获取日期与时间

  //计算天数差的函数,通用  
   function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2002-12-18格式  
       var  aDate,  oDate1,  oDate2,  iDays  
       aDate  =  sDate1.split("-")  
       oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2002格式  
       aDate  =  sDate2.split("-")  
       oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  
       iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
       return  iDays  
}

 

  1. /** 
  2.  * 优惠券有效期 
  3.  * startDate:起始日期 
  4.  * valueTime:有效天数 
  5.  */  
  6. function transferCouponValueTime(startDate,valueTime){  
  7.  var date = new Date(startDate);  
  8.  var newDate = new Date(date.getFullYear(),date.getMonth(),date.getDate()+valueTime);  
  9.  var year1 = date.getFullYear();  
  10.  var month1 = date.getMonth()+1;  
  11.  var day1 = date.getDate();  
  12.  var year2 = newDate.getFullYear();  
  13.  var month2 = newDate.getMonth()+1;  
  14.  var day2 = newDate.getDate();  
  15.  return "<span>"+year1+"</span>-<span>"+month1+"</span>-<span>"+day1+"</span>一<span>"  
  16.                         +year2+"</span>-<span>"+month2+"</span>-<span>"+day2+"</span>";  
  17.    
  18. }  

 

 

 

【获取某个元素的值】

var a=document.getElementById("operating_time").value;

【给某个元素赋值】

var b=document.getElementById("operating_time");
b.value=“”abc“”

 

【替换字符串】

//从字符串'Is this all there is'中剪去'is':
  var str='Is this all there is';


  var subStr=new RegExp('is');//创建正则表达式对象
  var result=str.replace(subStr,"");//把'is'替换为空字符串
  console.log(result);//Is th all there is


  var subStr=new RegExp('is','i');//创建正则表达式对象,不区分大小写
  var result=str.replace(subStr,"");//把'is'替换为空字符串
  console.log(result);//this all there is
    
  var subStr=new RegExp('is','ig');//创建正则表达式对象,不区分大小写,全局查找
  var result=str.replace(subStr,"");//把'is'替换为空字符串
  console.log(result);//th all there 


  var subStr=/is/ig;//直接量法创建正则表达式对象,不区分大小写,全局查找
  var result=str.replace(subStr,"");//把'is'替换为空字符串
  console.log(result);//th all there 

 

  console.log(str);//Is this all there is 可见replace并不改变原始str;

 

 

【隐藏站长统计】

 

在document.write(unescape("%3Cspan与id='cnzz_stat_icon_之间增加style='display:none;'

【文档完全载入后运行】

window.onload = function() {
//运行块写在这里
}

【获取浏览器地址参数】

http://aa.com/aaa?a=1

var objs = window.location.href.split("?")[1].split("&");

console.log(objs[0]);

【点击链接跳转】

document.getElementById("goToAction").onclick = function() {

 window.location = "https://aaa.com";

}

【jq】

定位<>:$("botton").click(function() {

定位id:$("#botton").click(function() {

【阻止表单提交】

<button class="layui-btn" lay-submit lay-filter="submitBotton" onclick="return false;">立即提交</button>

【将数字转为千分位】

function numFormat(num) {
    var res = num.toString().replace(/\d+/,
    function(n) { // 先提取整数部分
        return n.replace(/(\d)(?=(\d{3})+$)/g,
        function($1) {
            return $1 + ",";
        });
    }) return res;
}

【检查变量类型】

typeof(data_a)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值