各種常用的JS函數(shù)整理
更新時(shí)間:2013年10月25日 09:10:02 作者:
JS函數(shù)經(jīng)常使用的比較多比如獲取頁面地址參數(shù)、地址跳轉(zhuǎn)、判斷是否數(shù)字等等,在本文整理了一些,感興趣的可以參考下
Js獲取頁面地址參數(shù)
function getUrlPara(paraName)
{
var sUrl = location.href;
var sReg = "(?://?|&){1}" + paraName + "=([^&]*)"
var re = new RegExp(sReg, "gi");
re.exec(sUrl);
return RegExp.$1;
}
地址跳轉(zhuǎn)
var pn = $("#gotopagenum").val();//#gotopagenum是文本框的id屬性
location.href = "NewList.aspx?pagenum="+pn;//location.href實(shí)現(xiàn)客戶端頁面的跳轉(zhuǎn)
千分位
function Convert(money)
{
var s = money; //獲取小數(shù)型數(shù)據(jù)
s += "";
if (s.indexOf(".") == -1) s += ".00"; //如果沒有小數(shù)點(diǎn),在后面補(bǔ)個(gè)小數(shù)點(diǎn)和00
if (/\.\d$/.test(s)) s += "0"; //正則判斷
while (/\d{4}(\.|,)/.test(s)) //符合條件則進(jìn)行替換
s = s.replace(/(\d)(\d{3}(\.|,))/, "$1,$2"); //每隔3位添加一個(gè)
return s;
}
判斷是否數(shù)字
function IsNumeric(txt) {
if (txt == "") {
return false;
}
if (txt.indexOf(",") > 0) {
txt = txt.replace(",", "");
}
if (isNaN(txt)) {
return false;
}
else {
return true;
}
}
將數(shù)字進(jìn)行兩位小數(shù)的格式化
function changeTwoDecimal_f(x) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {
alert('function:changeTwoDecimal->parameter error');
return false;
}
f_x = Math.round(f_x * 100) / 100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + 2) {
s_x += '0';
}
return s_x;
}
Js 進(jìn)行數(shù)字運(yùn)算的函數(shù) parseFloat parseInt
js 當(dāng)前日期 yyyy-mm-dd 預(yù)置查詢條件
var now = new Date();
var year = now.getYear();
if (now.getYear() < 1900) {
year = now.getYear() + 1900;
}
var month = now.getMonth() + 1;
var day = now.getDate();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
$("#txtDate1").val(year.toString() + "-" + month.toString() + "-01");
$("#txtDate2").val(year.toString() + "-" + month.toString() + "-" + day.toString());
Js 獲取時(shí)間戳,在某些情景下代替Guid
function NowTimeCode()
{
var Result="";
var now = new Date();
var year = now.getYear();
if (now.getYear() < 1900) {
year = now.getYear() + 1900;
}
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minutes = now.getMinutes();
var second = now.getSeconds();
var millisecond = now.getMilliseconds();
if (month < 10) month = "0" + month;
if (day < 10) day = "0"+ day;
if (hour < 10) hour = "0"+ hour;
if (minutes < 10) minutes = "0"+ minutes;
if (second < 10) second = "0"+ second;
if (millisecond < 10)
millisecond = "00"+ millisecond;
else
{
if (millisecond < 100)
{
millisecond = "0"+ millisecond;
}
}
Result = year.toString() + month.toString() + day.toString() + hour.toString() + minutes.toString() + second.toString() + millisecond.toString();
return Result;
}
復(fù)制代碼 代碼如下:
function getUrlPara(paraName)
{
var sUrl = location.href;
var sReg = "(?://?|&){1}" + paraName + "=([^&]*)"
var re = new RegExp(sReg, "gi");
re.exec(sUrl);
return RegExp.$1;
}
地址跳轉(zhuǎn)
復(fù)制代碼 代碼如下:
var pn = $("#gotopagenum").val();//#gotopagenum是文本框的id屬性
location.href = "NewList.aspx?pagenum="+pn;//location.href實(shí)現(xiàn)客戶端頁面的跳轉(zhuǎn)
千分位
復(fù)制代碼 代碼如下:
function Convert(money)
{
var s = money; //獲取小數(shù)型數(shù)據(jù)
s += "";
if (s.indexOf(".") == -1) s += ".00"; //如果沒有小數(shù)點(diǎn),在后面補(bǔ)個(gè)小數(shù)點(diǎn)和00
if (/\.\d$/.test(s)) s += "0"; //正則判斷
while (/\d{4}(\.|,)/.test(s)) //符合條件則進(jìn)行替換
s = s.replace(/(\d)(\d{3}(\.|,))/, "$1,$2"); //每隔3位添加一個(gè)
return s;
}
判斷是否數(shù)字
復(fù)制代碼 代碼如下:
function IsNumeric(txt) {
if (txt == "") {
return false;
}
if (txt.indexOf(",") > 0) {
txt = txt.replace(",", "");
}
if (isNaN(txt)) {
return false;
}
else {
return true;
}
}
將數(shù)字進(jìn)行兩位小數(shù)的格式化
復(fù)制代碼 代碼如下:
function changeTwoDecimal_f(x) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {
alert('function:changeTwoDecimal->parameter error');
return false;
}
f_x = Math.round(f_x * 100) / 100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + 2) {
s_x += '0';
}
return s_x;
}
Js 進(jìn)行數(shù)字運(yùn)算的函數(shù) parseFloat parseInt
js 當(dāng)前日期 yyyy-mm-dd 預(yù)置查詢條件
復(fù)制代碼 代碼如下:
var now = new Date();
var year = now.getYear();
if (now.getYear() < 1900) {
year = now.getYear() + 1900;
}
var month = now.getMonth() + 1;
var day = now.getDate();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
$("#txtDate1").val(year.toString() + "-" + month.toString() + "-01");
$("#txtDate2").val(year.toString() + "-" + month.toString() + "-" + day.toString());
Js 獲取時(shí)間戳,在某些情景下代替Guid
復(fù)制代碼 代碼如下:
function NowTimeCode()
{
var Result="";
var now = new Date();
var year = now.getYear();
if (now.getYear() < 1900) {
year = now.getYear() + 1900;
}
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minutes = now.getMinutes();
var second = now.getSeconds();
var millisecond = now.getMilliseconds();
if (month < 10) month = "0" + month;
if (day < 10) day = "0"+ day;
if (hour < 10) hour = "0"+ hour;
if (minutes < 10) minutes = "0"+ minutes;
if (second < 10) second = "0"+ second;
if (millisecond < 10)
millisecond = "00"+ millisecond;
else
{
if (millisecond < 100)
{
millisecond = "0"+ millisecond;
}
}
Result = year.toString() + month.toString() + day.toString() + hour.toString() + minutes.toString() + second.toString() + millisecond.toString();
return Result;
}
相關(guān)文章
深入解讀JavaScript中的Hoisting機(jī)制
這篇文章主要介紹了JavaScript中的Hoisting機(jī)制,涉及到JS中變量聲明的相關(guān)問題,需要的朋友可以參考下2015-08-08javascript開發(fā)技術(shù)大全-第3章 js數(shù)據(jù)類型
字符串類型(string) :由unicode字符、數(shù)字、標(biāo)點(diǎn)符號(hào)組成,在javascript中沒有char字符類型 ,即使只表示一個(gè)字符,也必須用到字符串2011-07-07js實(shí)現(xiàn)各瀏覽器全屏代碼實(shí)例
本篇文章給大家分享了js實(shí)現(xiàn)各瀏覽器全屏的詳細(xì)代碼,有興趣的朋友可以參考學(xué)習(xí)下。2018-07-07javascript 繼承學(xué)習(xí)心得總結(jié)
下面小編就為大家?guī)硪黄猨avascript 繼承學(xué)習(xí)心得總結(jié)。小編覺得挺不錯(cuò)的?,F(xiàn)在分享給大家。給大家做個(gè)參考2016-03-03