JS函数经常使用的比较多比如获取页面地址参数、地址跳转、判断是否数字等等,在本文整理了一些,感兴趣的可以参考下
Js获取页面地址参数
function getUrlPara(paraName)
{
var sUrl = location.href;
var sReg = "(?://?|&){1}" + paraName + "=([^&]*)"
var re = new RegExp(sReg, "gi");
re.exec(sUrl);
return RegExp.$1;
}
地址跳转
千分位
判断是否数字
function IsNumeric(txt) {
if (txt == "") {
return false;
}
if (txt.indexOf(",") > 0) {
txt = txt.replace(",", "");
} // www.jbxue.com
if (isNaN(txt)) {
return false;
}
else {
return true;
}
}
将数字进行两位小数的格式化
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 进行数字运算的函数 parseFloat parseInt
js 当前日期 yyyy-mm-dd 预置查询条件
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 获取时间戳,在某些情景下代替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
{ // www.jbxue.com
if (millisecond < 100)
{
millisecond = "0"+ millisecond;
}
}
Result = year.toString() + month.toString() + day.toString() + hour.toString() + minutes.toString() + second.toString() + millisecond.toString();
return Result;
}
复制代码代码如下:
function getUrlPara(paraName)
{
var sUrl = location.href;
var sReg = "(?://?|&){1}" + paraName + "=([^&]*)"
var re = new RegExp(sReg, "gi");
re.exec(sUrl);
return RegExp.$1;
}
地址跳转
复制代码代码如下:
var pn = $("#gotopagenum").val();//#gotopagenum是文本框的id属性
location.href = "NewList.aspx?pagenum="+pn;//location.href实现客户端页面的跳转
location.href = "NewList.aspx?pagenum="+pn;//location.href实现客户端页面的跳转
千分位
复制代码代码如下:
function Convert(money)
{
var s = money; //获取小数型数据
s += "";
if (s.indexOf(".") == -1) s += ".00"; //如果没有小数点,在后面补个小数点和00
if (/\.\d$/.test(s)) s += "0"; //正则判断 // www.jbxue.com
while (/\d{4}(\.|,)/.test(s)) //符合条件则进行替换
s = s.replace(/(\d)(\d{3}(\.|,))/, "$1,$2"); //每隔3位添加一个
return s;
}
{
var s = money; //获取小数型数据
s += "";
if (s.indexOf(".") == -1) s += ".00"; //如果没有小数点,在后面补个小数点和00
if (/\.\d$/.test(s)) s += "0"; //正则判断 // www.jbxue.com
while (/\d{4}(\.|,)/.test(s)) //符合条件则进行替换
s = s.replace(/(\d)(\d{3}(\.|,))/, "$1,$2"); //每隔3位添加一个
return s;
}
判断是否数字
复制代码代码如下:
function IsNumeric(txt) {
if (txt == "") {
return false;
}
if (txt.indexOf(",") > 0) {
txt = txt.replace(",", "");
} // www.jbxue.com
if (isNaN(txt)) {
return false;
}
else {
return true;
}
}
将数字进行两位小数的格式化
复制代码代码如下:
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 进行数字运算的函数 parseFloat parseInt
js 当前日期 yyyy-mm-dd 预置查询条件
复制代码代码如下:
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 获取时间戳,在某些情景下代替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
{ // www.jbxue.com
if (millisecond < 100)
{
millisecond = "0"+ millisecond;
}
}
Result = year.toString() + month.toString() + day.toString() + hour.toString() + minutes.toString() + second.toString() + millisecond.toString();
return Result;
}
更多内容,请参考javascript脚本编程中的相关内容。
相关推荐
常用JavaScript函数整理。 正则表达式验证。
自己整理的高频JS函数,希望对大家有用,不错的话大家都下着玩吧
本文整理归纳了一些js常用函数,其中包括对数据操作,字符替换操作,日期及加解密操作函数,还有一些简单的验证函数。便于大家查阅参考。相信对大家会有所帮助。 1.typeof、constructor、instanceof对数组的判断 ...
** 描 述:公用函数 ** 版 本:V1.0 ******************************************************************/ //全局变量 g_sourceSubstr = "`~" //要替换的字串 g_objecSubstr = "\"" //替换为的字串 /***********...
整理的10大JavaScript函数,prototype的$函数,隐藏、显示元素函数,通过className获取DOM元素函数
主要介绍了JavaScript常用工具函数,汇总整理了各种JavaScript常用工具函数,包括获取、判断、转换、设置等相关功能函数的定义与使用方法,需要的朋友可以参考下
NULL 博文链接:https://ado88.iteye.com/blog/264464
自己整理的一个JS函数库 AJAX dom隐藏显示 数据整理 只有12KB 压缩后5kb 使用方法 引入文件 然后就可以直接使用了 具体函数方法如下 /* $(id) 根据元素id返回对象 N(name, i) 根据元素name返回对象,可能会有多个...
js 通用javascript函数库整理,学习js的朋友可以参考下。
javascript 开发之百度地图使用到的js函数整理 接项目用到的地图,客户要求用百度地图,没办法只好用百度地图,这里总结一下,写的一些函数,注释比较详细! //创建和初始化地图函数: function initMap(){ ...
1.JavaScript的数值处理对象学习 .txt 2.JavaScript的系统函数学习 .txt 3.js中用于对象的语句——with和for...in语句学习专题.txt 4.smallSoftkey小软键盘,大键盘 5.window.open参数详解 .txt 6....
业务中比较常用的JavaScript工具函数,可以用在前端环境,也可以用在node服务端,统一整理,方便查阅,帮助大家更好的理解和使用JavaScript,感兴趣的朋友可以了解下
javascript 验证函数集合: 收集整理了一些javascript常用的验证函数,需要的朋友可以下载。
随着做项目数量的越来越越多,其中用到js的地方很多相同,这里自己整理了一些常用表单验证的js方法,虽然和其他js验证框架有一定的差距,但是毕竟是自己总结的一些东西,在此与纪录分享一下。
前端业务中比较常用的JavaScript工具函数,浏览器环境常用,可直接拷贝在项目里使用。这里统一整理,方便查阅,本文章会持续更新。 一、file转为base64 /** * file转为base64 * @param {*} file file对象 * @...