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>

(二).跨语言字符串替换
System.Text.RegularExpressions.Regex.Replace(str1,@"<{1}[^<>]*>{1}","").Replace(" ","").Replace("\","//").Replace("-","-").Replace("&","&").Replace("<","<").Replace(">",">").Replace("br","/n.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.关闭本弹出窗体,并刷新父页面

       t
his
.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控件模板列.

(六). window.open() 方法
语法:window.open(pageurl,name,parameters);
window对象的open()方法用于创建一个新的窗口实例,新创建的窗口的外观由参数:parameters指定。新窗口中打开的文档由参数:         pageurl指定。系统能够根据参数:name确定的名称访问该窗口。

        下表为parameters参数表:
参数          衩始值           说明
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 设定新窗口距屏幕上方的距离

例子:
<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中主机名的字符串.
    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只收数字:09 , 也自可以定义其它可输入字符,如改成: 65123,只允许输入: az和AZ 等.

[以下为收藏]

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刷新、退格键


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
1&lt;script language=&quot;javascript&quot;&gt;  
2&lt;!--  
3  //屏蔽鼠标右键、Ctrl+N、Shift+F10、F5刷新、退格键  
4function window.onhelp(){return false} //屏蔽F1帮助  
5function KeyDown(){  
6  if ((window.event.altKey)&amp;&amp;  
7      ((window.event.keyCode==37)||   //屏蔽 Alt+ 方向键 ←  
8       (window.event.keyCode==39))){  //屏蔽 Alt+ 方向键 →  
9     alert(&quot;不准你使用ALT+方向键前进或后退网页!&quot;);  
10     event.returnvalue=false;  
11     }
12
13     /\* 注:这还不是真正地屏蔽 Alt+ 方向键,  
14     因为 Alt+ 方向键弹出警告框时,按住 Alt 键不放,  
15     用鼠标点掉警告框,这种屏蔽方法就失效了。以后若  
16     有哪位高手有真正屏蔽 Alt 键的方法,请告知。\*/
17
18  if ((event.keyCode == 8) &amp;&amp;   
19      (event.srcElement.type != &quot;text&quot; &amp;&amp;   
20      event.srcElement.type != &quot;textarea&quot; &amp;&amp;   
21      event.srcElement.type != &quot;password&quot;) ||           //屏蔽退格删除键     
22      (event.keyCode==116)||                            //屏蔽 F5 刷新键  
23      (event.ctrlKey &amp;&amp; event.keyCode==82)){            //Ctrl + R  
24     event.keyCode=0;  
25     event.returnvalue=false;  
26     }  
27  if ((event.ctrlKey)&amp;&amp;(event.keyCode==78))   //屏蔽 Ctrl+n  
28     event.returnvalue=false;  
29  if ((event.shiftKey)&amp;&amp;(event.keyCode==121)) //屏蔽 shift+F10  
30     event.returnvalue=false;  
31  if (window.event.srcElement.tagName == &quot;A&quot; &amp;&amp; window.event.shiftKey)   
32      window.event.returnvalue = false;  //屏蔽 shift 加鼠标左键新开一网页  
33  if ((window.event.altKey)&amp;&amp;(window.event.keyCode==115)){ //屏蔽Alt+F4  
34      window.showModelessDialog(&quot;about:blank&quot;,&quot;&quot;,&quot;dialogWidth:1px;dialogheight:1px&quot;);  
35      return false;}  
36  }  
37 /\* 另外可以用 window.open 的方法屏蔽 IE 的所有菜单  
38第一种方法:  
39  window.open(&quot;你的.htm&quot;, &quot;&quot;,&quot;toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,resizable=yes,status=no,top=0,left=0&quot;)  
40第二种方法是打开一个全屏的页面:  
41  window.open(&quot;你的.asp&quot;, &quot;&quot;, &quot;fullscreen=yes&quot;)  
42 \*/  
43//--&gt;  
44&lt;/script&gt;
45

1.4屏蔽浏览器右上角“最小化”“最大化”“关闭”键


1
2
3
4
5
6
7
8
9
10
1&lt;script language=javascript&gt;  
2function window.onbeforeunload()  
3{  
4  if(event.clientX&gt;document.body.clientWidth&amp;&amp;event.clientY&lt;0||event.altKey)  
5  {  
6    window.event.returnvalue = &quot;&quot;;  
7  }  
8}  
9&lt;/script&gt;
10

或者使用全屏打开页面


1
2
3
4
5
6
1&lt;script language=&quot;javascript&quot;&gt;  
2&lt;!--  
3window.open([www.32pic.com](http://www.32pic.com/),&quot;32pic&quot;,&quot;fullscreen=3,height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no&quot;);  
4--&gt;  
5&lt;/script&gt;
6

注:在body标签里加上onbeforeunload="javascript:return false"(使不能关闭窗口)

1.5屏蔽F5键


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1&lt;script language=&quot;javascript&quot;&gt;  
2&lt;!--  
3function document.onkeydown()   
4{   
5    if ( event.keyCode==116)   
6    {   
7        event.keyCode = 0;   
8        event.cancelBubble = true;   
9        return false;   
10    }  
11}  
12--&gt;  
13&lt;/script&gt;
14

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>表单提交验证类**

 

2.1 表单项不能为空


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1&lt;script language=&quot;javascript&quot;&gt;  
2&lt;!--  
3function CheckForm()  
4{  
5if (document.form.name.value.length == 0) {  
6   alert(&quot;请输入您姓名!&quot;);  
7   document.form.name.focus();  
8   return false;  
9}  
10   return true;  
11}  
12--&gt;  
13&lt;/script&gt;
14

2.2 比较两个表单项的值是否相同


1
2
3
4
5
6
7
8
9
10
11
12
13
1&lt;script language=&quot;javascript&quot;&gt;  
2&lt;!--  
3function CheckForm()  
4if (document.form.PWD.value != document.form.PWD_Again.value) {  
5   alert(&quot;您两次输入的密码不一样!请重新输入.&quot;);  
6   document.ADDUser.PWD.focus();  
7   return false;  
8}  
9   return true;  
10}  
11--&gt;  
12&lt;/script&gt;
13

2.3 表单项只能为数字和"_",用于电话/银行帐号验证上,可扩展到域名注册等


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
1&lt;script language=&quot;javascript&quot;&gt;  
2&lt;!--  
3function isNumber(String)  
4{   
5    var Letters = &quot;1234567890-&quot;; //可以自己增加可输入值  
6    var i;  
7    var c;  
8      if(String.charAt( 0 )==&#x27;-&#x27;)  
9 return false;  
10      if( String.charAt( String.length - 1 ) == &#x27;-&#x27; )  
11          return false;  
12     for( i = 0; i &lt; String.length; i ++ )  
13     {  
14          c = String.charAt( i );  
15   if (Letters.indexOf( c ) &lt; 0)  
16          return false;  
17}  
18     return true;  
19}  
20function CheckForm()  
21{  
22    if(! isNumber(document.form.TEL.value)) {  
23  alert(&quot;您的电话号码不合法!&quot;);  
24         document.form.TEL.focus();  
25         return false;  
26}  
27return true;  
28}  
29--&gt;  
30&lt;/script&gt;
31

2.4 表单项输入数值/长度限定


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1&lt;script language=&quot;javascript&quot;&gt;  
2&lt;!--  
3function CheckForm()   
4{  
5    if (document.form.count.value &gt; 100 || document.form.count.value &lt; 1)  
6{  
7 alert(&quot;输入数值不能小于零大于100!&quot;);  
8 document.form.count.focus();  
9 return false;  
10}  
11    if (document.form.MESSAGE.value.length&lt;10)  
12{  
13 alert(&quot;输入文字小于10!&quot;);  
14 document.form.MESSAGE.focus();  
15 return false;  
16}  
17return true;  
18}  
19//--&gt;  
20&lt;/script&gt;
21

2.5 中文/英文/数字/邮件地址合法性判断


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
1&lt;SCRIPT LANGUAGE=&quot;javascript&quot;&gt;  
2&lt;!--
3
4function isEnglish(name) //英文值检测  
5{  
6 if(name.length == 0)  
7  return false;  
8 for(i = 0; i &lt; name.length; i++) {  
9  if(name.charCodeAt(i) &gt; 128)  
10   return false;  
11 }  
12 return true;  
13}
14
15function isChinese(name) //中文值检测  
16{  
17 if(name.length == 0)  
18  return false;  
19 for(i = 0; i &lt; name.length; i++) {  
20  if(name.charCodeAt(i) &gt; 128)  
21   return true;  
22 }  
23 return false;  
24}
25
26function isMail(name) // E-mail值检测  
27{  
28 if(! isEnglish(name))  
29  return false;  
30 i = name.indexOf(&quot;@&quot;);  
31 j = name.lastIndexOf(&quot;@&quot;);  
32 if(i == -1)  
33  return false;  
34 if(i != j)  
35  return false;  
36 if(i == name.length)  
37  return false;  
38 return true;  
39}
40
41function isNumber(name) //数值检测  
42{  
43 if(name.length == 0)  
44  return false;  
45 for(i = 0; i &lt; name.length; i++) {  
46  if(name.charAt(i) &lt; &quot;0&quot; || name.charAt(i) &gt; &quot;9&quot;)  
47   return false;  
48 }  
49 return true;  
50}
51
52function CheckForm()  
53{  
54 if(! isMail(form.Email.value)) {  
55  alert(&quot;您的电子邮件不合法!&quot;);  
56  form.Email.focus();  
57  return false;  
58 }  
59 if(! isEnglish(form.name.value)) {  
60  alert(&quot;英文名不合法!&quot;);  
61  form.name.focus();  
62  return false;  
63 }  
64 if(! isChinese(form.cnname.value)) {  
65  alert(&quot;中文名不合法!&quot;);  
66  form.cnname.focus();  
67  return false;  
68 }  
69 if(! isNumber(form.PublicZipCode.value)) {  
70  alert(&quot;邮政编码不合法!&quot;);  
71  form.PublicZipCode.focus();  
72  return false;  
73 }  
74 return true;  
75}  
76//--&gt;  
77&lt;/SCRIPT&gt;
78

2.6 限定表单项不能输入的字符


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
1&lt;script language=&quot;javascript&quot;&gt;  
2&lt;!--
3
4function contain(str,charset)// 字符串包含测试函数  
5{  
6  var i;  
7  for(i=0;i&lt;charset.length;i++)  
8  if(str.indexOf(charset.charAt(i))&gt;=0)  
9  return true;  
10  return false;  
11}
12
13function CheckForm()  
14{  
15 if (
16(contain(document.form.NAME.value, &quot;%/(/)&gt;&lt;&quot;)) || 
17(contain(document.form.MESSAGE.value, &quot;%/(/)&gt;&lt;&quot;)))  
18{  
19  alert(&quot;输入了非法字符&quot;);  
20  document.form.NAME.focus();  
21  return false;  
22}  
23  return true;  
24}  
25//--&gt;  
26&lt;/script&gt;
27

 

 

 
40种网站设计常用技巧[引用]

  1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键

<table border oncontextmenu=return(false)><td>no</table> 可用于Table

  1. <body onselectstart="return false"> 取消选取、防止复制
  2. onpaste="return false" 不准粘贴

  3. oncopy="return false;" oncut="return false;" 防止复制

  4. <link rel="Shortcut Icon" href="favicon.ico"> IE地址栏前换成自己的图标
  5. <link rel="Bookmark" href="favicon.ico"> 可以在收藏夹中显示出你的图标
  6. <input style="ime-mode:disabled"> 关闭输入法

  7. 永远都会带着框架


1
2
3
4
1&lt;script language=&quot;JavaScript&quot;&gt;&lt;!--  
2if (window == top)top.location.href = &quot;frames.htm&quot;; //frames.htm为框架网页  
3// --&gt;&lt;/script&gt;
4
  1. 防止被人frame

1
2
3
4
1&lt;SCRIPT LANGUAGE=JAVASCRIPT&gt;&lt;!--  
2if (top.location != self.location)top.location=self.location;  
3// --&gt;&lt;/SCRIPT&gt;
4
  1. 网页将不能被另存为

1
2
1&lt;noscript&gt;&lt;iframe src=&quot;/blog/\*.html&gt;&quot;;&lt;/iframe&gt;&lt;/noscript&gt;
2
  1. <input type=button value=查看网页源代码

onclick="window.location = "view-source:"+ "链接地址"">

12.删除时确认
<a href="javascript:if(confirm("确实要删除吗?"))location="boos.asp?&areyou=删除&page=1"">删除</a>

  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
1//Javascript  
2&lt;script language=&quot;Javascript&quot;&gt;  
3function getIE(e){  
4var t=e.offsetTop;  
5var l=e.offsetLeft;  
6while(e=e.offsetParent)  
7alert(&quot;top=&quot;+t+&quot;/nleft=&quot;+l);  
8}  
9&lt;/script&gt;
10
11//VBScript  
12&lt;script language=&quot;VBScript&quot;&gt;&lt;!--  
13function getIE()  
14dim t,l,a,b  
15set a=document.all.img1  
16t=document.all.img1.offsetTop  
17l=document.all.img1.offsetLeft  
18while a.tagName&lt;&gt;&quot;BODY&quot;  
19set a = a.offsetParent  
20t=t+a.offsetTop  
21l=l+a.offsetLeft  
22wend  
23msgbox &quot;top=&quot;&amp;t&amp;chr(13)&amp;&quot;left=&quot;&amp;l,64,&quot;得到控件的位置&quot;  
24end function  
25--&gt;&lt;/script&gt;
26
  1. 光标是停在文本框文字的最后

1
2
3
4
5
6
7
8
9
10
11
12
1&lt;script language=&quot;javascript&quot;&gt;  
2function cc()  
3{  
4var e = event.srcElement;  
5var r =e.createTextRange();  
6r.moveStart(&quot;character&quot;,e.value.length);  
7r.collapse(true);  
8r.select();  
9}  
10&lt;/script&gt;  
11&lt;input type=text name=text1 value=&quot;123&quot; onfocus=&quot;cc()&quot;&gt;
12
  1. 判断上一页的来源

javascript:
document.referrer
16. 最小化、最大化、关闭窗口
<object id=hh1 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">

<param name="Command" value="Minimize"></object>
<object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Maximize"></object>
<OBJECT id=hh3 classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<PARAM NAME="Command" VALUE="Close"></OBJECT>
<input type=button value=最小化 onclick=hh1.Click()>
<input type=button value=最大化 onclick=hh2.Click()>
<input type=button value=关闭 onclick=hh3.Click()>
本例适用于IE

17.屏蔽功能键Shift,Alt,Ctrl

<script>
function look(){
if(event.shiftKey)
alert("禁止按Shift键!"); //可以换成ALT CTRL
}
document.onkeydown=look;
</script>

  1. 网页不会被缓存

<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
或者<META HTTP-EQUIV="expires" CONTENT="0">

19.怎样让表单没有凹凸感?
<input type=text style="border:1 solid \#000000">

<input type=text style="border-left:none; border-right:none; border-top:none; border-bottom:

1 solid #000000"></textarea>

20.<div><span>&<layer>的区别?

<div>(division)用来定义大段的页面元素,会产生转行
<span>用来定义同一行内的元素,跟<div>的唯一区别是不产生转行
<layer>是ns的标记,ie不支持,相当于<div>

21.让弹出窗口总是在最上面:

<body onblur="this.focus();">

22.不要滚动条?
让竖条没有:

<body style="overflow:scroll;overflow-y:hidden">
</body>
让横条没有:
<body style="overflow:scroll;overflow-x:hidden">
</body>
两个都去掉?更简单了
<body scroll="no">
</body>

23.怎样去掉图片链接点击后,图片周围的虚线?
<a href="\#" onFocus="this.blur()"><img src="/blog/logo.jpg" border=0></a>

24.电子邮件处理提交表单

<form name="form1" method="post" action="[链接地址](mailto:****@***.com)" enctype="text/plain">
<input type=submit>
</form>

25.在打开的子窗口刷新父窗口的代码里如何写?
window.opener.location.reload()

26.如何设定打开页面的大小

<body onload="top.resizeTo(300,200);">
打开页面的位置<body onload="top.moveBy(300,200);">

27.在页面中如何加入不是满铺的背景图片,拉动页面时背景图不动

<STYLE>
body
{background-image:url(/blog/logo.gif); background-repeat:no-repeat;
background-position:center;background-attachment: fixed}
</STYLE>

  1. 检查一段字符串是否全由数字组成

<script language="Javascript"><!–
function checkNum(str){return str.match(//D/)==null}
alert(checkNum("1232142141"))
alert(checkNum("123214214a1"))
// –></script>

  1. 获得一个窗口的大小

document.body.clientWidth; document.body.clientHeight

  1. 怎么判断是否是字符

if (/[^/x00-/xff]/g.test(s)) alert("含有汉字");
else alert("全是字符");

31.TEXTAREA自适应文字行数的多少

<textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.scrollHeight">
</textarea>

  1. 日期减去天数等于第二个日期

1
2
3
4
5
6
7
8
9
10
11
12
13
1&lt;script language=Javascript&gt;  
2function cc(dd,dadd)  
3{  
4//可以加上错误处理  
5var a = new Date(dd)  
6a = a.valueOf()  
7a = a - dadd \* 24 \* 60 \* 60 \* 1000  
8a = new Date(a)  
9alert(a.getFullYear() + &quot;年&quot; + (a.getMonth() + 1) + &quot;月&quot; + a.getDate() + &quot;日&quot;)  
10}  
11cc(&quot;12/23/2002&quot;,2)  
12&lt;/script&gt;
13
  1. 选择了哪一个Radio

1
2
3
4
5
6
7
8
9
10
11
12
1&lt;HTML&gt;&lt;script language=&quot;vbscript&quot;&gt;  
2function checkme()  
3for each ob in radio1  
4if ob.checked then window.alert ob.value  
5next  
6end function  
7&lt;/script&gt;&lt;BODY&gt;  
8&lt;INPUT name=&quot;radio1&quot; type=&quot;radio&quot; value=&quot;style&quot; checked&gt;Style  
9&lt;INPUT name=&quot;radio1&quot; type=&quot;radio&quot; value=&quot;barcode&quot;&gt;Barcode  
10&lt;INPUT type=&quot;button&quot; value=&quot;check&quot; onclick=&quot;checkme()&quot;&gt;  
11&lt;/BODY&gt;&lt;/HTML&gt;
12

34.脚本永不出错


1
2
3
4
5
6
7
8
9
1&lt;SCRIPT LANGUAGE=&quot;JavaScript&quot;&gt;  
2&lt;!-- Hide  
3function killErrors() {  
4return true;  
5}  
6window.onerror = killErrors;  
7// --&gt;  
8&lt;/SCRIPT&gt;
9

35.ENTER键可以让光标移到下一个输入框
<input onkeydown="if(event.keyCode==13)event.keyCode=9">
36. 检测某个网站的链接速度:
把如下代码加入<body>区域中:


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
1&lt;script language=Javascript&gt;  
2tim=1  
3setInterval(&quot;tim++&quot;,100)  
4b=1  
5var autourl=new Array()  
6autourl[1]=&quot;[www.njcatv.net](http://www.njcatv.net)&quot;  
7autourl[2]=&quot;javacool.3322.net&quot;  
8autourl[3]=&quot;[www.sina.com.cn](http://www.sina.com.cn)&quot;  
9autourl[4]=&quot;[www.nuaa.edu.cn](http://www.nuaa.edu.cn)&quot;  
10autourl[5]=&quot;[www.cctv.com](http://www.cctv.com)&quot;  
11function butt(){  
12document.write(&quot;&lt;form name=autof&gt;&quot;)  
13for(var i=1;i&lt;autourl.length;i++)  
14document.write(&quot;&lt;input type=text name=txt&quot;+i+&quot; size=10 value=&quot;/blog/测试中......&gt;&quot; =》&lt;input type=text  
15name=url&quot;+i+&quot; size=40&gt; =》&lt;input type=button value=GO
16
17onclick=window.open(this.form.url&quot;+i+&quot;.value)&gt;&lt;br/&gt;&quot;)  
18document.write(&quot;&lt;input type=submit value=刷新&gt;&lt;/form&gt;&quot;)  
19}  
20butt()  
21function auto(url)  
22else
23
24b++  
25}  
26function run(){for(var i=1;i&lt;autourl.length;i++)document.write(&quot;&lt;img src=http://&quot;+autourl+&quot;/&quot;+Math.random()+&quot; width=1 height=1
27
28onerror=auto(&quot;[链接地址](http://&quot;+autourl)+&quot;&quot;)&gt;&quot;)}  
29run()&lt;/script&gt;
30
  1. 各种样式的光标

auto :标准光标
default :标准箭头
hand :手形光标
wait :等待光标
text :I形光标
vertical-text :水平I形光标
no-drop :不可拖动光标
not-allowed :无效光标
help :?帮助光标
all-scroll :三角方向标
move :移动标
crosshair :十字标
e-resize
n-resize
nw-resize
w-resize
s-resize
se-resize
sw-resize

38.页面进入和退出的特效
进入页面<meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)">
推出页面<meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)"> 
这个是页面被载入和调出时的一些特效。duration表示特效的持续时间,以秒为单位。transition表示使用哪种特效,取值为1-23:
0 矩形缩小
1 矩形扩大
2 圆形缩小
3 圆形扩大
4 下到上刷新
5 上到下刷新
6 左到右刷新
7 右到左刷新
8 竖百叶窗
9 横百叶窗
10 错位横百叶窗
11 错位竖百叶窗
12 点扩散
13 左右到中间刷新
14 中间到左右刷新
15 中间到上下
16 上下到中间
17 右下到左上
18 右上到左下
19 左上到右下
20 左下到右上
21 横条
22 竖条
23 以上22种随机选择一种

39.在规定时间内跳转
<META http-equiv=V="REFRESH" content="5;URL=http://www.williamlong.info">

40.网页是否被检索
<meta name="ROBOTS" content="属性值">
其中属性值有以下一些:
属性值为"all": 文件将被检索,且页上链接可被查询;
属性值为"none": 文件不被检索,而且不查询页上的链接;
属性值为"index": 文件将被检索;
属性值为"follow": 查询页上的链接;
属性值为"noindex": 文件不检索,但可被查询链接;
属性值为"nofollow": 文件不被检索,但可查询页上的链接。

最大化窗口?


1
2
3
4
5
6
7
1&lt;script language=&quot;JavaScript&quot;&gt;  
2&lt;!--  
3self.moveTo(0,0)  
4self.resizeTo(screen.availWidth,screen.availHeight)  
5//--&gt;  
6&lt;/script&gt;
7

 

解决问题:由于层与下拉框之间的优先级是:下拉框 > 层,因此在显示的时候,会因为优先级的次序而会出现如上问题。(如果几个元素都是层的话,我们可以通过层的 z-index 属性来设置)解决办法就是:给层中放一个优先级比下拉框更高的元素(iframe),从而解决此问题!具体解决代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1&lt;div id=&quot;menu&quot; style=&quot;position:absolute; visibility:hidden; top:20px; left:20px; width:100px; height:200px; background-color:\#6699cc;&quot;&gt;  
2&lt;table&gt;  
3  &lt;tr&gt;&lt;td&gt;item 1&lt;/td&gt;&lt;/tr&gt;  
4  &lt;tr&gt;&lt;td&gt;item 2&lt;/td&gt;&lt;/tr&gt;  
5  &lt;tr&gt;&lt;td&gt;item 3&lt;/td&gt;&lt;/tr&gt;  
6  &lt;tr&gt;&lt;td&gt;item 4&lt;/td&gt;&lt;/tr&gt;  
7  &lt;tr&gt;&lt;td&gt;item 5&lt;/td&gt;&lt;/tr&gt;  
8  &lt;/table&gt;  
9  &lt;iframe src=&quot;/blog/javascript:false&quot; style=&quot;position:absolute; visibility:inherit; top:0px; left:0px; width:100px; height:200px; z-index:-1; filter=&#x27;progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)&#x27;;&quot;&gt;&lt;/iframe&gt;  
10&lt;/div&gt;
11
12&lt;a href=&quot;\#&quot; onclick=&quot;document.getElementById(&#x27;menu&#x27;).style.visibility=&#x27;visible&#x27;&quot;&gt;menu&lt;/a&gt;
13
14&lt;form&gt;  
15  &lt;select&gt;&lt;option&gt;A form selection list&lt;/option&gt;&lt;/select&gt;  
16&lt;/form&gt;
17

输入框也可以做的很漂亮了


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
1&lt;div align=&quot;center&quot;&gt;&lt;input type=&quot;hidden&quot; name=&quot;hao&quot; value=&quot;yes&quot;&gt;  
2              外向数:&lt;input  
3      name=answer  
4      style=&quot;color: rgb(255,0,0); border-left: medium none; border-right: medium none; border-top: medium none; border-bottom: 1px solid rgb(192,192,192)&quot;&gt;  
5             没回答的题数:&lt;input  
6      name=unanswer id=&quot;unanswer&quot;  
7      style=&quot;color: rgb(255,0,0); border-left: medium none; border-right: medium none; border-top: medium none; border-bottom: 1px solid rgb(192,192,192)&quot;&gt;  
8            &lt;br/&gt;  
9            总得分:  
10            &lt;input  
11      name=score id=&quot;score&quot;  
12      style=&quot;color: rgb(255,0,0); border-left: medium none; border-right: medium none; border-top: medium none; border-bottom: 1px solid rgb(192,192,192)&quot;&gt;  
13             结    论:  
14            &lt;input  
15      name=xgjg id=&quot;xgjg&quot;  
16      style=&quot;color: rgb(255,0,0); border-left: medium none; border-right: medium none; border-top: medium none; border-bottom: 1px solid rgb(192,192,192)&quot;&gt;  
17            &lt;br/&gt;  
18            &lt;br/&gt;  
19             
20            &lt;input onClick=processForm(this.form) style=&quot;FONT-FAMILY: 宋体; FONT-SIZE: 9pt&quot; type=button value=查看结果 name=&quot;button&quot;&gt;  
21             &lt;input type=&quot;reset&quot; name=&quot;Submit&quot; value=&quot;重做&quot;&gt;  
22            &lt;/div&gt;  
23注意:修改&lt;body&gt;为&lt;body onload=&quot;max.Click()&quot;&gt;即为打开最大
24
25化窗口,而如果改为&lt;body onload=&quot;min.Click()&quot;&gt;就变为窗口一打开就最小化
26
27&lt;object id=&quot;min&quot; type=&quot;application/x-oleobject&quot; classid=&quot;clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11&quot;&gt;  
28            &lt;param name=&quot;Command&quot; value=&quot;Minimize&quot;&gt;  
29          &lt;/object&gt; &lt;object id=&quot;max&quot; type=&quot;application/x-oleobject&quot; classid=&quot;clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11&quot;&gt;  
30            &lt;param name=&quot;Command&quot; value=&quot;Maximize&quot;&gt;  
31          &lt;/object&gt;  
32&lt;/body&gt;
33

页面自动刷新(说明)

当你做网页时,是不是有的时候想让你的网页自动不停刷新,或者过一段时间自动跳转到另外一个你自己设定的页面?其实实现这个效果非常地简单,而且这个效果甚至不能称之为特效。你只要把如下代码加入你的网页中就可以了。

1,页面自动刷新:把如下代码加入<head>区域中<meta http-equiv="refresh" content="20">,其中20指每隔20秒刷新一次页面.

2,页面自动跳转:把如下代码加入<head>区域中<meta http-equiv="refresh" content="20;url=http://www.daimajiaolou.com">,其中20指隔20秒后跳转到链接地址页面。

页面自动关闭

5000是指时间<body onLoad="setTimeout(window.close, 5000)">

弹出窗口自动关闭

10秒后弹出窗口自动关闭

注意:在新的tan.htm的body中要加 <onLoad="closeit()">
head


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
1&lt;script language=&quot;JavaScript&quot;&gt;
2
3&lt;!--
4
5var gt = unescape(&#x27;%3e&#x27;);
6
7var popup = null;
8
9var over = &quot;Launch Pop-up Navigator&quot;;
10
11popup = window.open(&#x27;&#x27;, &#x27;popupnav&#x27;, &#x27;width=225,height=235,resizable=1,scrollbars=auto&#x27;);
12
13if (popup != null) {
14
15if (popup.opener == null) {
16
17popup.opener = self;
18
19}
20
21popup.location.href = &#x27;tan.htm&#x27;;
22
23}
24
25// --&gt;
26
27&lt;/script&gt;  
28&lt;body&gt;注意:这段代码是在新建文件中的  
29&lt;script language=&quot;JavaScript&quot;&gt;
30
31 function closeit()
32
33 &lt;/script&gt;
34

这个可不是<iframe>(引用)呀。是直接调用的。以下代码加入<body>区域

<object type="text/x-scriptlet" width="800"  height="1000" data="../index.htm">
</object>

 

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ list, vector, map, set 区别与用法比较

2022-1-11 12:36:11

气候事件

中央气象台7月16日06时发布台风黄色预警

2014-7-16 9:12:33

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索