总结一些js自定义的函数
1、dayin()
作用:将id为dayin的内容,新建页面并打印,可解决打印某页面中的部分内容的问题。
使用方法:将要打印的内容通过 <span id="dayin"></span>包含起来,然后在某个按扭中定义事件
<input type="button" onclick="dayin()" value="打印">
function dayin() { var code="<body onload=window.print()>" code+=document.all.dayin.innerHTML; code=code.toUpperCase(); code=code.replace(/<A[^>]*>删除<\/A>/gi, ""); code=code.toLowerCase(); var newwin=window.open('','',''); newwin.opener = null; newwin.document.write(code); newwin.document.close(); } |
其中code=code.replace(/<A[^>]*>删除<\/A>/gi, "");
是过滤掉内容中的所有删除连接
是过滤掉内容中的所有删除连接
2、isNumber(st)
作用:判断变量st是否由数字组成(包括负数和小数),如果是返回true,否则返回false。
function isNumber(st) { var Letters = "1234567890-."; var i; var c; if(st.charAt( 0 )=='.') return false; if(st.charAt( 0 )=='-'&&st.charAt( 1 )=='.') return false; if( st.charAt( st.length - 1 ) == '-' ) return false; for( i = 0; i < st.length; i ++ ) { c = st.charAt( i ); if (Letters.indexOf( c ) < 0) return false; } return true; } |
3、createCookie(name,value,days)
作用:建立名称为name,值为values,有效期为days天的cookie。同时可用做修改。
function createCookie(name,value,days){ var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); expires = "; expires="+date.toGMTString(); }; document.cookie = name+"="+value+expires+"; path=/"; }; |
4、readCookie(name)
作用:根据名称,读取出cookie的值。如果无,则返回null。
function readCookie(name){ var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); }; return null; }; |
5、request(st)
作用:得到浏览器地址栏中的某个参数的值(不完美解决,例如有空格的话会得到%20,但支持中文)
function request(st) { var ustr=document.location.search; var intPos = ustr.indexOf("?"); var strRight = ustr.substr(intPos + 1); var arrTmp = strRight.split("%26"); for(var i = 0; i < arrTmp.length; i++) { var arrTemp = arrTmp[i].split("="); if(arrTemp[0].toUpperCase() == st.toUpperCase()) return arrTemp[1]; } return ""; } |
6、hideObject(obj)
作用:隐藏obj
function hideObject(obj) { obj.style.display = "none"; } |
7、showObject(obj)
作用:显示obj
function showObject(obj) { obj.style.display = "block"; } |
8、trim(str)
作用:去str两边空格
function trim(str) { return str.replace(/^\s*|\s*$/g,""); } |
9、function bj_date(d1,d2)
作用:比较d1,d2日期的大小
作用:比较d1,d2日期的大小
function bj_date(d1,d2) { /* author:wxg 作用:比较日期大小 参数:d1 d2 字符型 年-月-日 类型,如 2005-01-22 返回值: 0/1/2 数字型 d1>d2 返回0 d1=d2 返回1 d1<d2 返回2 */ if(d1==""&&d2==""){ return 3 } if(d1==""||d2==""){ return 4 } d1=d1.split("-") d2=d2.split("-") var a = new Date(Number(d1[0]),Number(d1[1]),Number(d1[2])) var b=new Date(Number(d2[0]),Number(d2[1]),Number(d2[2])) a = a.valueOf() b=b.valueOf() if(a-b>0) return 0 if(a-b==0) return 1 if(a-b<0) return 2 } |
10、格式化数字成货币格式
function setCurrency(s){
if(/[^0-9\.\-]/.test(s)) return "invalid value";
s=s.replace(/^(\d*)$/,"$1.");
s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
s=s.replace(".",",");
var re=/(\d)(\d{3},)/;
while(re.test(s))
s=s.replace(re,"$1,$2");
s=s.replace(/,(\d\d)$/,".$1");
return s.replace(/^\./,"0.")
}
if(/[^0-9\.\-]/.test(s)) return "invalid value";
s=s.replace(/^(\d*)$/,"$1.");
s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
s=s.replace(".",",");
var re=/(\d)(\d{3},)/;
while(re.test(s))
s=s.replace(re,"$1,$2");
s=s.replace(/,(\d\d)$/,".$1");
return s.replace(/^\./,"0.")
}
11、运行代码
function runCode(obj) {
var winname = window.open('', "_blank", '');
winname.document.open('text/html', 'replace');
winname.opener = null // 防止代码对论谈页面修改
winname.document.writeln(obj.value);
winname.document.close();
}
var winname = window.open('', "_blank", '');
winname.document.open('text/html', 'replace');
winname.opener = null // 防止代码对论谈页面修改
winname.document.writeln(obj.value);
winname.document.close();
}
12、保存代码
function saveCode(obj) {
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.writeln(obj.value);
winname.document.execCommand('saveas','','code.htm');
winname.close();
}
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.writeln(obj.value);
winname.document.execCommand('saveas','','code.htm');
winname.close();
}
function strLen(str) {
var count=0, asc, test='中文';
if (test.length==2) {
for (var i=0;i<str.length;i++) {
asc = str.charCodeAt(i);
if (asc < 0) asc += 65536;
if (asc > 255) count++;
}
}
return str.length+count;
}
var count=0, asc, test='中文';
if (test.length==2) {
for (var i=0;i<str.length;i++) {
asc = str.charCodeAt(i);
if (asc < 0) asc += 65536;
if (asc > 255) count++;
}
}
return str.length+count;
}
//按字节数截取左侧字符串
function strSubLeft(str, len) {
var count=0, asc, ret = "";
for (var i=0;i<str.length;i++) {
asc = str.charCodeAt(i);
if (asc < 0) asc += 65536;
if (asc > 255) count++;
if (i + count >= len) {
ret = str.substr(0, i);
break;
}
}
return ret;
}
function getRandom(){
return "&"+Math.random()*1000+"&";
}
function strSubLeft(str, len) {
var count=0, asc, ret = "";
for (var i=0;i<str.length;i++) {
asc = str.charCodeAt(i);
if (asc < 0) asc += 65536;
if (asc > 255) count++;
if (i + count >= len) {
ret = str.substr(0, i);
break;
}
}
return ret;
}
function getRandom(){
return "&"+Math.random()*1000+"&";
}
function checkInt(str){
if(str==null||str==""){
//alert("请输入数字!");
return false;
}
var digits = "1234567890";
var i = 0;
var strlen = str.length;
while((i<strlen)){
var char = str.charAt(i);
if(digits.indexOf(char) == -1) {
//alert("请输入正确的数字!");
return false;
}
i++;
}
return true;
}
function hasWord(str){
if(str.replace(/ /g,'')==""||str.replace(' ','')==""){
return false;
}else{
return true;
}
}
function replaceAll(oldStr,findStr,repStr){
var srchNdx = 0;
var newStr = "";
while (oldStr.indexOf(findStr,srchNdx) != -1){
newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
newStr += repStr;
srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
}
newStr += oldStr.substring(srchNdx,oldStr.length);
return newStr;
}
if(str==null||str==""){
//alert("请输入数字!");
return false;
}
var digits = "1234567890";
var i = 0;
var strlen = str.length;
while((i<strlen)){
var char = str.charAt(i);
if(digits.indexOf(char) == -1) {
//alert("请输入正确的数字!");
return false;
}
i++;
}
return true;
}
function hasWord(str){
if(str.replace(/ /g,'')==""||str.replace(' ','')==""){
return false;
}else{
return true;
}
}
function replaceAll(oldStr,findStr,repStr){
var srchNdx = 0;
var newStr = "";
while (oldStr.indexOf(findStr,srchNdx) != -1){
newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
newStr += repStr;
srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
}
newStr += oldStr.substring(srchNdx,oldStr.length);
return newStr;
}
function checkPrice(inputValue){
inputValue=""+inputValue;
myRegExp = /^(\d+),(\d+)$/;
if (inputValue.match(myRegExp)) return true;
else return false;
}
/*验证正整数*/
function checkNum(valueStr){
var myRegExp = /^(\d+)$/;
if (valueStr.match(myRegExp)){
return true;
}else{
return false;
}
}
/*限制obj(通常为TEXTAREA)中的字符个数*/
function checkWordsLength(obj,maxLength){
if(obj.value.length>=maxLength){
obj.value="http://blogbeta.blueidea.com/obj.value.substring(0,maxLength-1);
" }
}
function checkNum(valueStr){
var myRegExp = /^(\d+)$/;
if (valueStr.match(myRegExp)){
return true;
}else{
return false;
}
}
/*限制obj(通常为TEXTAREA)中的字符个数*/
function checkWordsLength(obj,maxLength){
if(obj.value.length>=maxLength){
obj.value="http://blogbeta.blueidea.com/obj.value.substring(0,maxLength-1);
" }
}
我来自:向东博客