JavaScript使用技巧精萃 经典代码收藏版
(一).确认删除用法:
1. BtnDel.Attributes.Add("onclick","return confirm('"+"确认删除?"+"')");
2. linktempDelete.Attributes["onclick"]="javascript:return confirm('"+"确认删除?"+"');";
3. private void grdProject_ItemDataBound(object sender, DataGridItemEventArgs e)
4.
{
if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
{
// 刪除按鈕上的提示部分
e.Item.Cells[10].Attributes.Add("onclick", "return confirm('确定删除吗?');");
}
}
5.
<script language="JavaScript" type="text/JavaScript">
function delete_y(e)
{
if(event.srcElement.outerText == "删除")
event.returnValue=confirm("确定删除?");
}
document.onclick=delete_y;
</script>
(二).跨语言字符串替换
1 | System.Text.RegularExpressions.Regex.Replace(str1,@ " <{1}[^ <>]*>{1}" , "" ).Replace( " " , "" ).Replace( "\"," \\ ").Replace(" - "," - ").Replace(" & "," & ").Replace(" < "," < ").Replace(" > "," > ").Replace(" br "," \n"); |
(三).关闭窗体
1.
this.btnClose.Attributes.Add("onclick", "window.close();return false;");
2.关闭本窗体间跳转到另一个页面
this.HyperLink1.NavigateUrl = "javascript:onclick=window.opener.location.assign
('" + "index.aspx"+ "?&Func=Edit&AutoID=" + intAutoid + ');window.close();";
3.关闭父窗体:
<INPUT type="reset" value=' <%=this._Cancel%>' onclick="window.parent.close()" Class="Button">
4.关闭本弹出窗体,并刷新父页面
this.Response.Write(" <script language='javascript'>window.opener.location.reload();window.close(); </script>");
(四).Web MessageBox
1.
Response.Write(" <script language=javascript> window.alert('保存成功1'); </script>");
2.
Response.Write(" <script>alert('"+"保存成功!"+"'); </script>");
3.
this.Page.RegisterStartupScript("ChiName"," <script language='javascript'>alert('" + "保存成功!" + "') </script>");
(五).DataGrid中加CheckBox控件模板列.
请看: http://blog.csdn.net/chengking/archive/2005/10/08/497520.aspx
(六). window.open() 方法
语法:window.open(pageurl,name,parameters);
window对象的open()方法用于创建一个新的窗口实例,新创建的窗口的外观由参数:parameters指定。新窗口中打开的文档由参数: pageurl指定。系统能够根据参数:name确定的名称访问该窗口。
下表为parameters参数表:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | alwaysLowered yes/no 指定窗口隐藏在所有窗口之下。 alwaysRaised yes/no 指定窗口浮在所有窗口之上。 dependent yes/no 指定打开的窗口为父窗口的一个了窗口。并随父窗口的关闭而关闭。 directions yes/no 指定Navigator 2和3的目录栏是否在新窗口中可见。 height pixel value 设定新窗口的像素高度。 hotkeys yes/no 在没有菜单栏的新窗口设置安全退出热键。 innerHeight pixel value 设置新窗口中文档的像素高度。 innerWidth pixel value 设置新窗口中文档的像素宽度。 location yes/ no 指明位置栏在新窗口中是否可见。 menubar yes /no 指明菜单栏在新窗口中是否可见。 outerHeight pixel value 设定窗口(包括装饰边框)的像素高度。 outerWidth pixel value 设定窗口(包括装饰边框)的像素宽度。 resizable yes /no 指明新窗口是否可以调整。 screenX pixel value 设定新窗口离屏幕边界的像素长度。 screenY pixel value 设定新窗口离屏幕上边界的像素长度。 scrollbars yes /no 指明滚动栏在新窗口中是否可见。 titlebar yes /no 指明菜单题目栏在新窗口是否可见。 toolbar yes /no 指明工具栏在新窗口中是否可见。 Width pixel value 设定窗口的像素宽度。 z-look yes /no 在文档中包含各个 <pplet>标签的数组。 fullscreen yes / no 打开的窗体是否进行全屏显示 left pixel value 设定新窗口距屏幕左方的距离 top pixel value 设定新窗口距屏幕上方的距离 |
例子:
1 2 3 4 5 6 7 8 9 10 11 12 | <html> <head> <title>window.open函数 </title> </head> <body> <script language= "javascript" > <!-- window.open( "" , "name1" , "width=100,height=200,toolbar=no,scrollbars=no,menubar=no,screenX=100,screenY=100" ); //--> </script> </body> </html> |
(七).location对象
href 整个URL字符串.
protocol 含有URL第一部分的字符串,如http:
host 包含有URL中主机名:端口号部分的字符串.如//www.cenpok.net/server/
hostname 包含URL中主机名的字符串.如http://www.cenpok.net
port 包含URL中可能存在的端口号字符串.
pathname URL中"/"以后的部分.如~list/index.htm
hash "#"号(CGI参数)之后的字符串.
search "?"号(CGI参数)之后的字符串.
(八).按键捕捉
1.Ctrl+Enter按键捕捉方法
<body onkeydown="doKeyDown()">
<script language="JavaScript">
<!--
function doKeyDown()
{
if (event.ctrlKey && event.keyCode == 13)
{
alert("You pressed the Ctrl + Enter")
}
}
//-->
</script>
</body>
2.Alt加快捷键: Alt+A
<button accessKey=A title="Alt+A" onclick="alert('Button clicked!')">Alt+A </button>
(九).控制输入,非法字符不能输入到TextBox.
<asp:textbox class="Text"
onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;"
id="txtY_Revenue" style="TEXT-ALIGN: right" runat="server" Width="90%" MaxLength="12">
</asp:textbox>
说明: 此方法控制TextBox只收数字:0~9 , 也自可以定义其它可输入字符,如改成: 65~123,只允许输入: a~z和A~Z 等.
1>屏蔽功能类
1.1 屏蔽键盘所有键
<script language="javascript">
<!--
function document.onkeydown(){
event.keyCode = 0;
event.returnvalue = false;
}
-->
</script>
1.2 屏蔽鼠标右键
在body标签里加上oncontextmenu=self.event.returnvalue=false
或者
<script language="javascript">
<!--
function document.oncontextmenu()
{
return false;
}
-->
</script>
function nocontextmenu()
{
if(document.all) {
event.cancelBubble=true;
event.returnvalue=false;
return false;
}
}
或者
<body onmousedown="rclick()" oncontextmenu= "nocontextmenu()">
<script language="javascript">
<!--
function rclick()
{
if(document.all) {
if (event.button == 2){
event.returnvalue=false;
}
}
}
-->
</script>
1.3 屏蔽 Ctrl+N、Shift+F10、F5刷新、退格键
<script language="javascript">
<!--
//屏蔽鼠标右键、Ctrl+N、Shift+F10、F5刷新、退格键
function window.onhelp(){return false} //屏蔽F1帮助
function KeyDown(){
if ((window.event.altKey)&&
((window.event.keyCode==37)|| //屏蔽 Alt+ 方向键 ←
(window.event.keyCode==39))){ //屏蔽 Alt+ 方向键 →
alert("不准你使用ALT+方向键前进或后退网页!");
event.returnvalue=false;
}
/* 注:这还不是真正地屏蔽 Alt+ 方向键,
因为 Alt+ 方向键弹出警告框时,按住 Alt 键不放,
用鼠标点掉警告框,这种屏蔽方法就失效了。以后若
有哪位高手有真正屏蔽 Alt 键的方法,请告知。*/
if ((event.keyCode == 8) &&
(event.srcElement.type != "text" &&
event.srcElement.type != "textarea" &&
event.srcElement.type != "password") || //屏蔽退格删除键
(event.keyCode==116)|| //屏蔽 F5 刷新键
(event.ctrlKey && event.keyCode==82)){ //Ctrl + R
event.keyCode=0;
event.returnvalue=false;
}
if ((event.ctrlKey)&&(event.keyCode==78)) //屏蔽 Ctrl+n
event.returnvalue=false;
if ((event.shiftKey)&&(event.keyCode==121)) //屏蔽 shift+F10
event.returnvalue=false;
if (window.event.srcElement.tagName == "A" && window.event.shiftKey)
window.event.returnvalue = false; //屏蔽 shift 加鼠标左键新开一网页
if ((window.event.altKey)&&(window.event.keyCode==115)){ //屏蔽Alt+F4
window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px");
return false;}
}
/* 另外可以用 window.open 的方法屏蔽 IE 的所有菜单
第一种方法:
window.open("你的.htm", "","toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,resizable=yes,status=no,top=0,left=0")
第二种方法是打开一个全屏的页面:
window.open("你的.asp", "", "fullscreen=yes")
*/
//-->
</script>
1.4屏蔽浏览器右上角“最小化”“最大化”“关闭”键
<script language=javascript>
function window.onbeforeunload()
{
if(event.clientX>document.body.clientWidth&&event.clientY <0||event.altKey)
{
window.event.returnvalue = "";
}
}
</script>
或者使用全屏打开页面
<script language="javascript">
<!--
window.open(www.32pic.com,"32pic","fullscreen=3,height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
-->
</script>
注:在body标签里加上onbeforeunload="javascript:return false"(使不能关闭窗口)
1.5屏蔽F5键
<script language="javascript">
<!--
function document.onkeydown()
{
if ( event.keyCode==116)
{
event.keyCode = 0;
event.cancelBubble = true;
return false;
}
}
-->
</script>
1.6屏蔽IE后退按钮
在你链接的时候用 <a href="javascript:location.replace(url)">
1.7屏蔽主窗口滚动条
在body标签里加上 style="overflow-y:hidden"
1.8 屏蔽拷屏,不断地清空剪贴板
在body标签里加上onload="setInterval('clipboardData.setData(\'Text\',\'\')',100)"
1.9 屏蔽网站的打印功能
<style>
@media print {
* { display: none }
}
</style>
1.10 屏蔽IE6.0 图片上自动出现的保存图标
方法一:
<META HTTP-EQUIV="imagetoolbar" CONTENT="no">
方法二:
<img galleryimg="no">
1.11 屏蔽页中所有的script
<noscrript> </noscript>
2>表单提交验证类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | 2.1 表单项不能为空 <script language= "javascript" > <!-- function CheckForm() { if (document.form.name.value.length == 0) { alert( "请输入您姓名!" ); document.form.name.focus(); return false ; } return true ; } --> </script> 2.2 比较两个表单项的值是否相同 <script language= "javascript" > <!-- function CheckForm() if (document.form.PWD.value != document.form.PWD_Again.value) { alert( "您两次输入的密码不一样!请重新输入." ); document.ADDUser.PWD.focus(); return false ; } return true ; } --> </script> 2.3 表单项只能为数字和 "_" ,用于电话/银行帐号验证上,可扩展到域名注册等 <script language= "javascript" > <!-- function isNumber(String) { var Letters = "1234567890-" ; //可以自己增加可输入值 var i; var c; if (String.charAt( 0 )== '-' ) return false ; if ( String.charAt( String.length - 1 ) == '-' ) return false ; for ( i = 0; i < String.length; i ++ ) { c = String.charAt( i ); if (Letters.indexOf( c ) < 0) return false ; } return true ; } function CheckForm() { if (! isNumber(document.form.TEL.value)) { alert( "您的电话号码不合法!" ); document.form.TEL.focus(); return false ; } return true ; } --> </script> 2.4 表单项输入数值/长度限定 <script language= "javascript" > <!-- function CheckForm() { if (document.form.count.value > 100 || document.form.count.value < 1) { alert( "输入数值不能小于零大于100!" ); document.form.count.focus(); return false ; } if (document.form.MESSAGE.value.length <10) { alert( "输入文字小于10!" ); document.form.MESSAGE.focus(); return false ; } return true ; } //--> </script> 2.5 中文/英文/数字/邮件地址合法性判断 <SCRIPT LANGUAGE= "javascript" > <!-- function isEnglish(name) //英文值检测 { if (name.length == 0) return false ; for (i = 0; i < name.length; i++) { if (name.charCodeAt(i) > 128) return false ; } return true ; } function isChinese(name) //中文值检测 { if (name.length == 0) return false ; for (i = 0; i < name.length; i++) { if (name.charCodeAt(i) > 128) return true ; } return false ; } function isMail(name) // E-mail值检测 { if (! isEnglish(name)) return false ; i = name.indexOf( "@" ); j = name.lastIndexOf( "@" ); if (i == -1) return false ; if (i != j) return false ; if (i == name.length) return false ; return true ; } function isNumber(name) //数值检测 { if (name.length == 0) return false ; for (i = 0; i < name.length; i++) { if (name.charAt(i) < "0" || name.charAt(i) > "9" ) return false ; } return true ; } function CheckForm() { if (! isMail(form.Email.value)) { alert( "您的电子邮件不合法!" ); form.Email.focus(); return false ; } if (! isEnglish(form.name.value)) { alert( "英文名不合法!" ); form.name.focus(); return false ; } if (! isChinese(form.cnname.value)) { alert( "中文名不合法!" ); form.cnname.focus(); return false ; } if (! isNumber(form.PublicZipCode.value)) { alert( "邮政编码不合法!" ); form.PublicZipCode.focus(); return false ; } return true ; } //--> </SCRIPT> 2.6 限定表单项不能输入的字符 <script language= "javascript" > <!-- function contain(str,charset) // 字符串包含测试函数 { var i; for (i=0;i <charset.length;i++) if (str.indexOf(charset.charAt(i))>=0) return true ; return false ; } function CheckForm() { if ((contain(document.form.NAME.value, "%\(\)> <" )) || (contain(document.form.MESSAGE.value, "%\(\)> <" ))) { alert( "输入了非法字符" ); document.form.NAME.focus(); return false ; } return true ; } //--> </script> |
补充1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | ■打开■ <input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开> <OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0> </OBJECT> ■另存为■ <input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为> <OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0> </OBJECT> ■属性■ <input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=属性> <OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0> </OBJECT> ■打印■ <input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印> <OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0> </OBJECT> ■页面设置■ <input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置> <OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0> </OBJECT> ■刷新■ <input type=button value=刷新 name=refresh onclick= "window.location.reload()" > ■导入收藏■ <input type= "button" name= "Button" value= "导入收藏夹" onClick=window.external.ImportExportFavorites( true ,);> ■导出收藏■ <input type= "button" name= "Button3" value= "导出收藏夹" onClick=window.external.ImportExportFavorites( false ,);> ■加入收藏■ <INPUT name=Button2 onclick= "window.external.AddFavorite(location.href, document.title)" type=button value=加入收藏夹> ■整理收藏夹■ <INPUT name=Submit2 onclick= "window.external.ShowBrowserUI(OrganizeFavorites, null)" type=button value=整理收藏夹> ■查看原文件■ <INPUT name=Button onclick=window.location = "view-source:" + window.location.href type=button value=查看源文件> ■语言设置■ <INPUT name=Button onclick="window.external.ShowBrowserUI(LanguageDialojavadetype=button value=语言设置> ■前进■ <INPUT name=Submit onclick=history.go(1) type=submit value=前进> ■后退■ <INPUT name=Submit2 onclick=history.go(-1) type=submit value=后退> |
按钮10秒后才可以点击.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <input name= "rulesubmit" id= "agree" class = "button" type= "submit" value= "我已阅读并同意以上条款" > <input type= "button" name= "return" class = "button" value= "不同意" onclick= "javascript:window.location='/opencms/index.html';" ></div> </form> <script language= "javascript" > var secs = 10; var wait = secs * 1000; document.bbrules.rulesubmit.value = "我已阅读并同意以上条款 (" + secs + ")" ; document.bbrules.rulesubmit.disabled = true ; for (i = 1; i <= secs; i++) { window.setTimeout( "update(" + i + ")" , i * 1000); } window.setTimeout( "timerrun()" , wait); function update(num, value) { if (num == (wait/1000)) { document.bbrules.rulesubmit.value = "我已阅读并同意以上条款" ; } else { printnr = (wait / 1000)-num; document.bbrules.rulesubmit.value = "我已阅读并同意以上条款 (" + printnr + ")" ; } } function timerrun() { document.bbrules.rulesubmit.disabled = false ; document.bbrules.rulesubmit.value = "我已阅读并同意以上条款" ; } </script> |
关于浏览器关闭的捕捉事件
1 2 3 4 5 6 7 8 9 10 | <script language=javascript> window.onbeforeunload = function () { if (event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey) { window.event.returnValue = "" ; //your code } } </script> |
链接超时
把如下代码加入区域中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <script language=Javascript> tim=1 setInterval( "tim++" ,100) b=1 var autourl= new Array() autourl[1]= "www.njcatv.net" autourl[2]= "javacool.3322.net" autourl[3]= "www.sina.com.cn" autourl[4]= "www.nuaa.edu.cn" autourl[5]= "www.cctv.com" function butt(){ document.write( "<form ) for(var i=1;i<autourl.length;i++) document.write(" <input type=text +i+ " size=10 value=测试中……> =》<input type=text name=url" +i+ " size=40> =》<input type=button value=GO onclick=window.open(this.form.url" +i+ ".value)> " ) document.write( "<input type=submit value=刷新></form>" ) } butt() function auto(url){ document.forms[0][ "url" +b].value=url if (tim>200) {document.forms[0][ "txt" +b].value= "链接超时" } else {document.forms[0][ "txt" +b].value= "时间" +tim/10+ "秒" } b++ } function run(){ for ( var i=1;i<autourl.length;i++)document.write( " onerror=auto(" http: //"+autourl+"")>")} run()</script> |
解决内存泄漏的问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <script language= "javascript" > function sendRequest(url) { url += (url.indexOf( "?" )==-1? "?" : "&" ) + "timestamp=" + new Date().getTime(); req = new ActiveXObject( "Microsoft.XMLHTTP" ); req.open( "GET" ,url, true ); writeStuff( 'Opening ' + url); req.onreadystatechange = function () { writeStuff( 'readyState is now ' + req.readyState); } req.send( null ); writeStuff( 'Sending request' ); } function writeStuff(msg) { log.innerHTML += msg + " " ; } </script> <div id= "log" ></div> |
作者:【唐】三三
出处:https://www.cnblogs.com/tangge/archive/2011/06/23/2087724.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具