常用Js代码汇总

来源:互联网 发布:淘宝怎么搜烟 编辑:程序博客网 时间:2024/04/26 05:25

1、textarea 换行 的问题,替换后显示出来就是换行的了


function strReplace(str) {//替换回车换行           return str.replace(/(\r)*\n/g,"<br/>").replace(/\s/g," "); } //t


2、获取网页链接的制定参数

function getPara(pa){//获取网页参数var str=location.href;var pos=str.lastIndexOf("?");if (str.lastIndexOf(pa)==-1) {return 1;}{if(pos<0) return;if(pos==str.length-1) return;str=str.substr(pos+1);var ary=str.split("&");var len=ary.length;for(i=0;i<len;i++){var para=ary[i].split("=");if (para[0]==pa){return para[1];break;}}}}

3、去除空格,修改中文“,”为英文“,”,用于限制自动修正用户的输入

function FormatChar(text) //去除空格,修改中文“,”为英文“,”{    var tmp_gs = text    var re = /,/g;  //采用正则表达式    tmp_gs = tmp_gs.replace(re, ",");    re = /( )/g;    tmp_gs = tmp_gs.replace(re, "");    return tmp_gs;}
4、截取文件的扩展名



//截取文件的扩展名function getFileExt(CurrentFilename) {    var index1 = CurrentFilename.lastIndexOf(".");    if (index1 != -1) {        var index2 = CurrentFilename.length;        return CurrentFilename.substring(index1 + 1, index2);    }}


5、毫秒转换为时分秒

function MillisecondToDate(msd) {    var time = parseFloat(msd) / 1000;    if (null != time && "" != time) {        if (time > 60 && time < 60 * 60) {            time = parseInt(time / 60.0) + "分钟" + parseInt((parseFloat(time / 60.0) -            parseInt(time / 60.0)) * 60) + "秒";        } else if (time >= 60 * 60 && time < 60 * 60 * 24) {            time = parseInt(time / 3600.0) + "小时" + parseInt((parseFloat(time / 3600.0) -            parseInt(time / 3600.0)) * 60) + "分钟" +            parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -            parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + "秒";        } else {            time = parseInt(time) + "秒";        }    } else {        time = "0 时 0 分0 秒";    }    return time;}  



原创粉丝点击