免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3905 | 回复: 10
打印 上一主题 下一主题

常用js, 不断更新中... [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-20 19:11 |只看该作者 |倒序浏览
1.日期大小比较  
var regS = new RegExp("-","gi");  
var bvalue=document.all("today").value.replace(regS,"/");  
var evalue=document.all("providerConfirmValidity").value.replace(regS,"/");  
var d1 = new Date(bvalue);  
var d2 = new Date(evalue);  
if(Date.parse(d1) - Date.parse(d2)>=0){  
    alert("有效确认时间必须大于当前时间 !");  
    document.all("providerConfirmValidity").focus();  
    return false;  
}

论坛徽章:
0
2 [报告]
发表于 2011-11-20 19:12 |只看该作者
2.只能输入数字的文本框(非数字自动回退)。  
<input type='text' name='netMatCount' size='8' onkeyup="this.value=this.value.replace(/[^/d/.]/g,'')" onbeforepaste="clipboardData.setData  
('text',clipboardData.getData('text').replace(/^(-|/+)?(/d+/./d+)$/,''))" />  
var price = /^/d+(/./d{1,4})?$/;  
if(!price.test(form1.netMatCount.value)){  
    alert("请填写需求数量 !");  
    return false;  
}  
/**  
*   文本框只允许输入数字或小数点时调用此方法。  
*   obj:文本框对象  
*/   
function clearNoNum(obj){   
    if(inNaN(obj.value)) {   
        obj.value = obj.value.replace(/[^/d.]/g,""); //先把非数字的都替换掉,除了数字和.   
        obj.value = obj.value.replace(/^/./g,"");//必须保证第一个为数字而不是.   
        obj.value = obj.value.replace(//.{2,}/g,"."); //保证只有出现一个.而没有多个.   
         
        //保证.只出现一次,而不能出现两次以上   
        obj.value = obj.value.replace(".","$#$").replace(//./g,"").replace("$#$",".");   
    }   
}

论坛徽章:
0
3 [报告]
发表于 2011-11-20 19:13 |只看该作者
3.一组复选框,至少必须有一项选中  
function selectOne(checkboxName,describe){  
    for(var i=1;i<checkboxName.length;i++){  
        if(checkboxName[i].checked==true){  
            return true;  
        }  
    }  
    alert("请选择" + describe + "!");  
    return false;  
}

论坛徽章:
0
4 [报告]
发表于 2011-11-20 19:13 |只看该作者
4.验证一个在某个区间的正整数  
function isAreaInt(myString,min,max){  
    if(!isInt(myString)){  
        return false;  
    }  
    if(parseInt(myString)>max||parseInt(myString)<min){  
        return false;  
    }  
    return true;  
}

论坛徽章:
0
5 [报告]
发表于 2011-11-20 19:14 |只看该作者
5.打开一个窗口,使窗口出现在屏幕中心位置,有滚动条,窗口大小可变  
function winOpen(fileName,winName,winWidth,winHeight){  
    var winName=window.open(fileName,winName,"top=" + (screen.height-winHeight)/2 + ",left=" + (screen.width-winWidth)/2 + ",width=" + winWidth +   
",height=" + winHeight + ",scrollbars=yes,resizable=yes,location=no,toolbar=no,status=no");  
    winName.focus();  
}

论坛徽章:
0
6 [报告]
发表于 2011-11-20 19:15 |只看该作者
6.打开一个窗口,使窗口出现在屏幕中心位置,窗口大小固定,有滚动条  
function winOpenSizeBar(fileName,winName,winWidth,winHeight){  
    var winName=window.open(fileName,winName,"top=" + (screen.height-winHeight)/2 + ",left=" + (screen.width-winWidth)/2 + ",width=" + winWidth +   
",height=" + winHeight + ",scrollbars=yes,resizable=no,location=no,toolbar=no,status=no");  
    winName.focus();  
}

论坛徽章:
0
7 [报告]
发表于 2011-11-20 19:16 |只看该作者
7.高亮显示表格中的一行  
function choose_color(tableId,row,newColor){  
    var bgColor = "#E1FFFF";  
    if (newColor&&newColor != "")  
    {  
        bgColor = newColor;  
    }  
    var obj = document.getElementById(tableId);  
    var rowCount = obj.rows.length;  
    for (var i=0;i< rowCount; i++){  
        obj.rows(i).style.backgroundColor = "";  
    }  
    row.style.backgroundColor = bgColor;  
}  
<tr height="30" align="right" onmousemove="choose_color('firstTable',this)">

论坛徽章:
0
8 [报告]
发表于 2011-11-20 19:17 |只看该作者
8.去掉字串两边的空格  
function trim(str)  
{  
    return lTrim(rTrim(str));  
}  
  
9.email的判断。  
function ismail(mail)  
{  
        return(new RegExp(/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/).test(mail));  
}

论坛徽章:
0
9 [报告]
发表于 2011-11-20 19:18 |只看该作者
10.高亮替换  
window.onload = function() {  
    for( var i = 0; i < document.getElementsByTagName("ol").length; i++ ) {  
        nav(document.getElementsByTagName("ol")[i]);  
    }  
    function nav(obj) {   
        for( var i = 0; i < obj.getElementsByTagName("li").length; i++ ) {  
            obj.getElementsByTagName("li")[i].onmousemove = function() {  
                this.style.background = "#f8f8f8";  
                this.style.border= "3px solid #e2762e";  
            }  
            obj.getElementsByTagName("li")[i].onmouseout = function() {  
                this.style.background = "#ffffff";  
                this.style.border= "1px solid #dbdbdb";  
            }  
        }  
    }  
}

论坛徽章:
0
10 [报告]
发表于 2011-11-20 19:19 |只看该作者
11.Table隔行换色、移动换色  
    <mce:script language="javascript"><!--  
        function senfe(o,a,b,c,d){  
            var t=document.getElementById(o).getElementsByTagName("tr");  
              
            for(var i=0;i<t.length;i++){  
                t[i].style.backgroundColor=(t[i].sectionRowIndex%2==0)?a:b;  
                t[i].onclick=function(){  
                    if(this.x!="1"){  
                        this.x="1";  
                        this.style.backgroundColor=d;  
                    }else{  
                        this.x="0";  
                        this.style.backgroundColor=(this.sectionRowIndex%2==0)?a:b;  
                    }  
                }  
                t[i].onmouseover=function(){  
                    if(this.x!="1")this.style.backgroundColor=c;  
                }  
                t[i].onmouseout=function(){  
                    if(this.x!="1")this.style.backgroundColor=(this.sectionRowIndex%2==0)?a:b;  
                }  
            }  
        }  
// --></mce:script>   
    <mce:script language="javascript"><!--  
        //senfe("表格名称","奇数行背景","偶数行背景","鼠标经过背景","点击后背景");  
        senfe("changecolor","#f3f8fd","#ffffff","#ADADAD","#f391a9");  
// --></mce:script>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP