栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

javascript常用的方法分享

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

javascript常用的方法分享

针对现在大家平时开发中,都会写一些重复性的js处理代码,今天总结了几个比较常用的方法实现。获取get请求参数、去字符串空格。

1、获取get请求中的参数

Js代码 

function getPara(para){  
  if(location.href.indexOf("?") == -1){  
   // 没有参数,则Do nothing.  
   return null;  
  }  
  else{  
   // 取得GET请求?号后面的字符串  
   var urlQuery = location.href.split("?");  
   if(urlQuery[1].indexOf("&")==-1){//只有一个参数  
    if (urlQuery[1].indexOf("=") == -1) {  
     //没有等号,没有参数,则Do nothing  
     return null;  
    }else{  
     var keyValue = urlQuery[1].split("=");  
     var key   = keyValue[0];  
     var value  = keyValue[1];  
     if(key==para){  
      return value;  
     }  
    }  
   }else{  
    // 解析参数  
    var urlTerms = urlQuery[1].split("&");  
    for (var i = 0; i 

2、 //本函数用于去掉字符串左边的空格 

Js代码 

function leftTrim(str) {  
  if (str.charAt(0) == " ") {  
    str = str.slice(1);  
    str = leftTrim(str);  
  }  
   
  return str;  
}  

3、 //本函数用于去掉字符串右边的空格 

Js代码 

function rightTrim(str) {  
  if (str.length - 1 >= 0 && str.charAt(str.length - 1) == " ") {  
    str = str.slice(0, str.length - 1);  
    str = rightTrim(str);  
  }  
   
  return str;  
} 

4、 //将时间转换成固定格式输出 

Js代码

  
Date.prototype.toFormatString=function(format){  
  var formatstr = format;  
  if(format != null && format != ""){  
    //设置年  
    if(formatstr.indexOf("yyyy") >=0 ){  
      formatstr = formatstr.replace("yyyy",this.getFullYear());  
    }  
    //设置月  
    if(formatstr.indexOf("MM") >=0 ){  
      var month = this.getMonth() + 1;  
      if(month < 10){  
 month = "0" + month;  
      }  
      formatstr = formatstr.replace("MM",month);  
    }  
    //设置日  
    if(formatstr.indexOf("dd") >=0 ){  
      var day = this.getDay();  
      if(day < 10){  
 day = "0" + day;  
      }  
      formatstr = formatstr.replace("dd",day);  
    }  
    //设置时 - 24小时  
    var hours = this.getHours();  
    if(formatstr.indexOf("HH") >=0 ){  
      if(month < 10){  
 month = "0" + month;  
      }  
      formatstr = formatstr.replace("HH",hours);  
    }  
    //设置时 - 12小时  
    if(formatstr.indexOf("hh") >=0 ){  
      if(hours > 12){  
 hours = hours - 12;  
      }  
      if(hours < 10){  
 hours = "0" + hours;  
      }  
      formatstr = formatstr.replace("hh",hours);  
    }  
    //设置分  
    if(formatstr.indexOf("mm") >=0 ){  
      var minute = this.getMinutes();  
      if(minute < 10){  
 minute = "0" + minute;  
      }  
      formatstr = formatstr.replace("mm",minute);  
    }  
    //设置秒  
    if(formatstr.indexOf("ss") >=0 ){  
      var second = this.getSeconds();  
      if(second < 10){  
 second = "0" + second;  
      }  
      formatstr = formatstr.replace("ss",second);  
    }  
  }  
  return formatstr;  
} 

以上所述就是本文的全部内容了,希望大家能够喜欢。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/101788.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号