js常⽤代码⼤全
Javascript常⽤代码⼤全
//打开模式对话框
<body><script language=javascript>
function doSelectUser(txtId){
strFeatures="dialogWidth=500px;dialogHeight=360px;center=yes;middle=yes ;help=no;status=no;scroll=no";
var url,strReturn;
url="selUser.aspx";
strReturn=window.showModalDialog(url,',strFeatures');
} doSelectUser()
</script></body>
//返回模式对话框的值
function okbtn_onclick() {
var commstr='';
window.close() ;
} okbtn_onclick()
//全屏幕打开 IE 窗⼝
var winWidth=screen.availWidth ;
var winHeight=screen.availHeight-20;
window.open("main.aspx","surveyWindow","toolbar=no,width="+ winWidth  +",height="+ winHeight +",top=0,left=0,scrollbars=yes,resizable=yes,center:yes,statusbars=yes"); //脚本中中使⽤xml
function initialize() {
var xmlDoc
var xslDoc
xmlDoc = new ActiveXObject('Microsoft.XMLDOM')
xmlDoc.async = false;
xslDoc = new ActiveXObject('Microsoft.XMLDOM')
xslDoc.async = false;
xmlDoc.load("l")
xslDoc.load("tree.xsl")
folderTree.innerHTML = ansformNode(xslDoc)
}
⼀、验证类
1、数字验证内
1.1 整数
1.2 ⼤于0的整数(⽤于传来的ID的验证)
1.3 负整数的验证
1.4 整数不能⼤于iMax
1.5 整数不能⼩于iMin
2、时间类
2.1 短时间,形如 (13:04:06)
2.2 短⽇期,形如 (2003-12-05)
2.3 长时间,形如 (2003-12-05 13:04:06)
2.4 只有年和⽉。形如(2003-05,或者2003-5)
2.5 只有⼩时和分钟,形如(12:03)
3、表单类
3.1 所有的表单的值都不能为空
3.2 多⾏⽂本框的值不能为空。
3.3 多⾏⽂本框的值不能超过sMaxStrleng
3.4 多⾏⽂本框的值不能少于sMixStrleng
3.5 判断单选框是否选择。
3.6 判断复选框是否选择.
3.7 复选框的全选,多选,全不选,反选
3.8 ⽂件上传过程中判断⽂件类型
4、字符类
4.1 判断字符全部由a-Z或者是A-Z的字字母组成
4.2 判断字符由字母和数字组成。
4.3 判断字符由字母和数字,下划线,点号组成.且开头的只能是下划线和字母
4.4 字符串替换函数.Replace();
5、浏览器类
5.1 判断浏览器的类型
5.2 判断ie的版本
5.3 判断客户端的分辨率
6、结合类
6.1 email的判断。
6.2 ⼿机号码的验证
6.3 ⾝份证的验证
⼆、功能类
1、时间与相关控件类
1.1 ⽇历
1.2 时间控件
1.3 万年历
1.4 显⽰动态显⽰时钟效果(⽂本,如OA中时间)
1.5 显⽰动态显⽰时钟效果 (图像,像⼿表)
2、表单类
2.1 ⾃动⽣成表单
2.2 动态添加,修改,删除下拉框中的元素
2.3 可以输⼊内容的下拉框
2.4 多⾏⽂本框中只能输⼊iMax⽂字。如果多输⼊了,⾃动减少到iMax个⽂字(多⽤于短信发送)
3、打印类
3.1 打印控件
4、事件类
4.1 屏蔽右键
4.2 屏蔽所有功能键
4.3 --> 和<-- F5 F11,F9,F1
4.4 屏蔽组合键ctrl+N
5、⽹页设计类
5.1 连续滚动的⽂字,图⽚
(注意是连续的,两段⽂字和图⽚中没有空⽩出现)
5.2 html编辑控件类
5.3 颜⾊选取框控件
5.4 下拉菜单
5.5 两层或多层次的下拉菜单
5.6 仿IE菜单的按钮。
(效果如rongshuxa的导航栏⽬)
5.7 状态栏,title栏的动态效果
(例⼦很多,可以研究⼀下)
5.8 双击后,⽹页⾃动滚屏
6、树型结构。
6.1 asp+SQL版
6.2 asp+xml+sql版
6.3 java+sql或者java+sql+xml
7、⽆边框效果的制作
8、连动下拉框技术
9、⽂本排序
10,画图类,含饼、柱、⽮量贝滋曲线
11,操纵客户端注册表类
12,DIV层相关(拖拽、显⽰、隐藏、移动、增加)
13,TABLAE相关
(客户端动态增加⾏列,模拟进度条,滚动列表等)
14,各种<object classid=>相关类,如播放器,flash与脚本互动等
16, 刷新/模拟⽆刷新异步调⽤类(XMLHttp或iframe,frame
⼀、验证类
1、数字验证内
1.1 整数  /^(-|\+)?\d+$/.test(str)
1.2 ⼤于0的整数(⽤于传来的ID的验证)
/^\d+$/.test(str)
1.3 负整数的验证    /^-\d+$/.test(str)
2、时间类
2.1 短时间,形如 (13:04:06)
function isTime(str)  {
var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
if (a == null) {
alert('输⼊的参数不是时间格式');
return false;}
if (a[1]>24 || a[3]>60 || a[4]>60)  {
alert("时间格式不对");
return false;    }
return true;    }
document.write (isTime('15:04:06'))
返回值:true
2.2 短⽇期,形如 (2003-12-05)
function strDateTime(str)  {
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&Date()==r[4]);    }
document.write (strDateTime("2003-12-05"))
返回值:true
2.3 长时间,形如 (2003-12-05 13:04:06)
function strDateTime(str)  {
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&Date()==r[4]&&Hours()==r[5]&&Minutes()==r[6]&&Seconds()==r[7]);      }
document.write (strDateTime("2003-12-05 13:04:06"))
返回值:true
2.4 只有年和⽉。形如(2003-05,或者2003-5)
2.5 只有⼩时和分钟,形如(12:03)
3、表单类
3.1 所有的表单的值都不能为空
<input onblur="if(place(/^ +| +$/g,')==')alert('不能为空!')">
3.2 多⾏⽂本框的值不能为空。
3.3 多⾏⽂本框的值不能超过sMaxStrleng
3.4 多⾏⽂本框的值不能少于sMixStrleng
3.5 判断单选框是否选择。
3.6 判断复选框是否选择.
3.7 复选框的全选,多选,全不选,反选
3.8 ⽂件上传过程中判断⽂件类型
4、字符类
4.1 判断字符全部由a-Z或者是A-Z的字字母组成
<input onblur="if(/[^a-zA-Z]/g.test(this.value))alert('有错')">
4.2 判断字符由字母和数字组成。
<input onblur="if(/[^0-9a-zA-Z]/g.test(this.value))alert('有错')">
4.3 判断字符由字母和数字,下划线,点号组成.且开头的只能是下划线和字母 /^([a-zA-z_]{1})([\w]*)$/g.test(str)
4.4 字符串替换函数.Replace();
5、浏览器类
5.1 判断浏览器的类型window.navigator.appName
5.2 判断ie的版本 window.navigator.appVersion
5.3 判断客户端的分辨率
window.screen.height;  window.screen.width;
6、结合类
6.1 email的判断。
function ismail(mail) {
return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail));  }
6.2 ⼿机号码的验证
6.3 ⾝份证的验证
function isIdCardNo(num) {
if (isNaN(num)) {alert("输⼊的不是数字!"); return false;}
var len = num.length, re;
if (len == 15)
re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
else if (len == 18)
re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);
else {alert("输⼊的数字位数不对!"); return false;}
var a = num.match(re);
if (a != null) {
if (len==15) {
var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&Date()==a[5];  }
else{
var D = new Date(a[3]+"/"+a[4]+"/"+a[5]);
var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&Date()==a[5];  }
if (!B) {alert("输⼊的⾝份证号 "+ a[0] +" ⾥出⽣⽇期不对!"); return false;}
}
return true;  }
document.write (isIdCardNo("110108************"))
返回值:true
画图(⼀个矩形框):
<OBJECT
id=S
height=240
width=392
classid="clsid:369303C2-D7AC-11D0-89D5-00A0C90833E6">
</OBJECT>
<SCRIPT>
S.DrawingSurface.ArcDegrees(0,0,0,30,50,60);
S.DrawingSurface.ArcRadians(30,0,0,30,50,60);
S.DrawingSurface.Line(10,10,100,100);
</SCRIPT>
写注册表:
<SCRIPT>
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.RegWrite ("HKCU\ oftware\\ACME\\FortuneTeller\\", 1, "REG_BINARY");
WshShell.RegWrite ("HKCU\ oftware\\ACME\\FortuneTeller\\MindReader", "Goocher!", "REG_SZ");
var bKey =    WshShell.RegRead ("HKCU\ oftware\\ACME\\FortuneTeller\\");
WScript.Echo (WshShell.RegRead ("HKCU\ oftware\\ACME\\FortuneTeller\\MindReader"));
WshShell.RegDelete ("HKCU\ oftware\\ACME\\FortuneTeller\\MindReader");
WshShell.RegDelete ("HKCU\ oftware\\ACME\\FortuneTeller\\");
WshShell.RegDelete ("HKCU\ oftware\\ACME\\");
</SCRIPT>
TABLAE相关(客户端动态增加⾏列)
<HTML>
<SCRIPT LANGUAGE="JScript">
function numberCells() {
var count=0;
for (i=0; i < ws.length; i++) {
for (j=0; j < ws(i).cells.length; j++) {
ws(i).cells(j).innerText = count;
count++;  }  }  }
</SCRIPT>
<BODY onload="numberCells()">
<TABLE id=mytable border=1>
<TR><TH> </TH><TH> </TH><TH> </TH><TH> </TH></TR>
<TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
<TR><TD> </TD><TD> </TD><TD> </TD><TD> </TD></TR>
</TABLE>
</BODY>
</HTML>
1.⾝份证严格验证:
<script>
var aCity={11:"北京",12:"天津",13:"河北",14:"⼭西",15:"内蒙古",21:"辽宁",22:"吉林",23:"⿊龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"⼭东",41:"河南",42:"湖北",43:"湖南",44:"⼴东",45:"⼴西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"⽢肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"⾹港",82:"澳门",91:"国外"}
function cidInfo(sId){
var iSum=0
var info=""
if(!/^\d{17}(\d|x)$/i.test(sId))return false;
place(/x$/i,"a");
if(aCity[parseInt(sId.substr(0,2))]==null)return "Error:⾮法地区";
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
var d=new place(/-/g,"/"))
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "Error:⾮法⽣⽇";
for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
if(iSum%11!=1)return "Error:⾮法证号";
return aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"⼥")  }
document.write(cidInfo("380524198002300016"),"<br/>");
document.write(cidInfo("340524************"),"<br/>")
document.write(cidInfo("340524************"),"<br/>")
document.write(cidInfo("340524************"),"<br/>");
</script>
2.验证IP地址
<SCRIPT LANGUAGE="JavaScript">
function isip(s){
var check=function(v){try{return (v<=255 && v>=0)}catch(x){return false}};
var re=s.split(".")
return (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false  }
var s="202.197.78.129";
alert(isip(s))
</SCRIPT>
3.加sp1后还能⽤的⽆边框窗⼝!!
<HTML XMLNS:IE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<IE:Download ID="include" STYLE="behavior:url(#default#download)" />
<title>Chromeless Window</title>
<SCRIPT LANGUAGE="JScript">
/*--- Special Thanks For andot ---*/
/*  This following code are designed and writen by Windy_sk <seasonx@163>
You can use it freely, but u must held all the copyright items!
*/
/*--- Thanks For andot Again ---*/
var CW_width= 400;
var CW_height= 300;
var CW_top= 100;
var CW_left= 100;
var CW_url= "/";
var New_CW= atePopup();
var CW_Body= New_CW.document.body;
var content= "";
var CSStext= "margin:1px;color:black; border:2px outset;border-style:expression(onmouseout=onmouseup=function(){this.style.borderStyle='outset'}, onmousedown=function() {if(event.button!=2)this.style.borderStyle='inset'});background-color:buttonface;width:16px;height:14px;font-size:12px;line-height:11px;cursor:Default;";
//Build Window
include.startDownload(CW_url, function(source){content=source});
function insert_content(){
var temp = "";
CW_Body.style.overflow= "hidden";
CW_Body.style.backgroundColor= "white";
CW_Body.style.border=  "solid black 1px";
content = place(/<a ([^>]*)>/g,"<a onclick='parent.open(this.href);return false' $1>");
temp += "<table width=100% height=100% cellpadding=0 cellspacing=0 border=0>";
temp += "<tr style=';font-size:12px;background:#0099CC;height:20;cursor:default'
ondblclick=\"Max.innerText=Max.innerText=='1'?'2':'1';parent.if_max=!parent.if_max;parent.show_CW();\" onmouseup='parent.drag_up(event)'
onmousemove='parent.drag_move(event)' onmousedown='parent.drag_down(event)' onselectstart='return false' oncontextmenu='return false'>";
temp += "<td style='color:#ffffff;padding-left:5px'>Chromeless Window For IE6 SP1</td>";
temp += "<td style='color:#ffffff;padding-right:5px;' align=right>";
temp += "<span id=Help  onclick=\"alert('Chromeless Window For IE6 SP1  -  Ver 1.0\\n\\nCode By Windy_sk\\n\\nSpecial Thanks For andot')\" style=\""+CSStext+"font-family:System;padding-right:2px;\">?</span>";
temp += "<span id=Min  onclick='parent.New_CW.hide();parent.blur()' style=\""+CSStext+"font-family:Webdings;\" title='Minimum'>0</span>";
temp += "<span id=Max  onclick=\"this.innerText=this.innerText=='1'?'2':'1';parent.if_max=!parent.if_max;parent.show_CW();\" style=\""+CSStext+"font-family:Webdings;\"
title='Maximum'>1</span>";
temp += "<span id=Close onclick='parent.opener=null;parent.close()' style=\""+CSStext+"font-family:System;padding-right:2px;\" title='Close'>x</span>";
temp += "</td></tr><tr><td colspan=2>";
temp += "<div id=include style='overflow:scroll;overflow-x:hidden;overflow-y:auto; HEIGHT: 100%; width:"+CW_width+"'>";
temp += content;
temp += "</div>";代码运行js特效
temp += "</td></tr></table>";
CW_Body.innerHTML = temp;  }
setTimeout("insert_content()",1000);
var if_max = true;
function show_CW(){
if(if_max){
New_CW.show(CW_top, CW_left, CW_width, CW_height);
if(typeof(New_CW.document.all.include)!="undefined"){
New_CW.document.all.include.style.width = CW_width;
New_CW.document.all.Max.innerText = "1";  }
}else{
New_CW.show(0, 0, screen.width, screen.height);
New_CW.document.all.include.style.width = screen.width;
}  }
// Move Window
var drag_x,drag_y,draging=false
function drag_move(e){
if (draging){
New_CW.show(e.screenX-drag_x, e.screenY-drag_y, CW_width, CW_height);
return false;  }  }
function drag_down(e){
if(e.button==2)return;
if(New_CW.document.body.offsetWidth==screen.width && New_CW.document.body.offsetHeight==screen.height)
retur n;
drag_x=e.clientX;
drag_y=e.clientY;
draging=true;
e.srcElement.setCapture();  }
function drag_up(e){
draging=false;
leaseCapture();
if(New_CW.document.body.offsetWidth==screen.width && New_CW.document.body.offsetHeight==screen.height) return; CW_top  = e.screenX-drag_x;
CW_left = e.screenY-drag_y;  }
</SCRIPT>
</HTML>
贴两个关于treeview的
<script language="javascript">
<!--
//初始化选中节点
function initchecknode() {
var TreeNode("1");
node.setAttribute("Checked","true");
setcheck(node,"true");
FindCheckedFromNode(TreeView1);  }
//oncheck事件
function tree_oncheck(tree) {
var TreeNode(tree.clickedNodeIndex);
var TreeNode(tree.clickedNodeIndex).getAttribute("checked");
setcheck(node,Pchecked);
document.all.checked.value="";
document.all.unchecked.value="";
FindCheckedFromNode(TreeView1);  }
//设置⼦节点选中
function setcheck(node,Pc) {
var i;
var ChildNode=new Array();
Children();
if(parseInt(ChildNode.length)==0)
return;
else  {
for(i=0;i<ChildNode.length;i++)  {
var cNode;
cNode=ChildNode[i];
if(Children().length)!=0)
setcheck(cNode,Pc);
cNode.setAttribute("Checked",Pc);  }  }  }
//获取所有节点状态
function FindCheckedFromNode(node) {
var i = 0;
var nodes = new Array();
nodes = Children();
for (i = 0; i < nodes.length; i++) {
var cNode;
cNode=nodes[i];
if (Attribute("Checked"))
AddChecked(cNode);
else
AddUnChecked(cNode);
if (Children().length) != 0 ) {
FindCheckedFromNode(cNode);
}  }  }
//添加选中节点
function AddChecked(node) {
document.all.checked.value += Attribute("NodeData");
document.all.checked.value += ',';  }
//添加未选中节点
function AddUnChecked(node) {
document.all.unchecked.value += Attribute("NodeData");
document.all.unchecked.value += ',';  }
//--> </script>
treeview中如何在服务器端得到客户端设置后的节点选中状态
<script language="" runat="server">
private void Button1_Click(object sender, System.EventArgs e) {
Response.Write(TreeView1.Nodes[0].Checked);  }
</script>
<script language="javascript">
function set_check()  {
var nodeindex = "0";
var TreeNode(nodeindex);
node.setAttribute("Checked","True");
TreeView1.queueEvent('oncheck', nodeindex);  }
</script>
三個實⽤的⼩技巧:關閉輸⼊法.禁⽌貼上.禁⽌複製
關閉輸⼊法
本⽂字框輸⼊法被關閉:
語法:
範例: <input type="text" name="textfield" >
禁⽌貼上
本⽂字框禁⽌貼上⽂字:
語法:onpaste="return false"
範例:<input type="text" name="textfield" onpaste="return false">
禁⽌複製
本⽂字框禁⽌複製:
語法:oncopy="return false;" oncut="return false;"
範例:<input name="textfield" type="text" value="不能複製裡⾯的字" oncopy="return false;" oncut="return false;">
//================================
//Cookie操作
//================================
function getCookieVal (offset) {
var endstr = kie.indexOf (";", offset);
if (endstr == -1)
endstr = kie.length;
return kie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。