论坛首页 Web前端技术论坛

40种网页常用小技巧(JavaScript)

浏览 2809 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2007-04-10  
虽然比较简单  但在需要的时候  找不到 还真的很麻烦  今儿给大家列这  经常搞这些东西的  留一份以备不时之需 也很不错
 
  1. 40种网页常用小技巧(JavaScript)       
  2.    
  3.  1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键  
  4. <table border oncontextmenu=return(false)><td>no</table> 可用于Table  
  5.   
  6. 2. <body onselectstart="return false"> 取消选取、防止复制  
  7.   
  8. 3. onpaste="return false" 不准粘贴  
  9.   
  10. 4. oncopy="return false;" oncut="return false;" 防止复制  
  11.   
  12. 5. <link rel="Shortcut Icon" href="favicon.ico"> IE地址栏前换成自己的图标  
  13.   
  14. 6. <link rel="Bookmark" href="favicon.ico"> 可以在收藏夹中显示出你的图标  
  15.   
  16. 7. <input style="ime-mode:disabled"> 关闭输入法  
  17.   
  18. 8. 永远都会带着框架  
  19. <script language="JavaScript"><!--  
  20. if (window == top)top.location.href = "frames.htm"//frames.htm为框架网页  
  21. // --></script>  
  22.   
  23. 9. 防止被人frame  
  24. <SCRIPT LANGUAGE=JAVASCRIPT><!--  
  25. if (top.location != self.location)top.location= self.location;  
  26. // --></SCRIPT>  
  27.   
  28. 10. 网页将不能被另存为  
  29. <noscript><iframe src=*.html></iframe></noscript>  
  30.   
  31. 11. <input type=button value=查看网页源代码  
  32. onclick="window.location = "view-source:"+ " http://www.pconline.com.cn"">  
  33.   
  34. 12.删除时确认  
  35. <a href="javascript:if(confirm("确实要删除吗?"))location="boos.asp?&areyou=删除&page=1"">删除</a>  
  36.   
  37. 13. 取得控件的绝对位置  
  38. //Javascript  
  39. <script language="Javascript">  
  40. function getIE(e){  
  41. var t=e.offsetTop;  
  42. var l=e.offsetLeft;  
  43. while(e=e.offsetParent){  
  44. t+=e.offsetTop;  
  45. l+=e.offsetLeft;  
  46. }  
  47. alert("top="+t+"/nleft="+l);  
  48. }  
  49. </script>  
  50.   
  51. //VBScript  
  52. <script language="VBScript"><!--  
  53. function getIE()  
  54. dim t,l,a,b  
  55. set a=document.all.img1  
  56. t=document.all.img1.offsetTop  
  57. l=document.all.img1.offsetLeft  
  58. while a.tagName<>"BODY"  
  59. set a = a.offsetParent  
  60. t=t+a.offsetTop  
  61. l=l+a.offsetLeft  
  62. wend  
  63. msgbox "top="&t&chr(13)&"left="&l,64,"得到控件的位置"  
  64. end function  
  65. --></script>  
  66.   
  67. 14. 光标是停在文本框文字的最后  
  68. <script language="javascript">  
  69. function cc()  
  70. {  
  71. var e = event.srcElement;  
  72. var r =e.createTextRange();  
  73. r.moveStart("character", e.value.length);  
  74. r.collapse(true);  
  75. r.select();  
  76. }  
  77. </script>  
  78. <input type=text name=text1 value="123" onfocus="cc()">  
  79.   
  80. 15. 判断上一页的来源  
  81. javascript:  
  82. document.referrer  
  83.   
  84. 16. 最小化、最大化、关闭窗口  
  85. <object id=hh1 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">  
  86. <param name="Command" value="Minimize"></object>  
  87. <object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">  
  88. <param name="Command" value="Maximize"></object>  
  89. <OBJECT id=hh3 classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">  
  90. <PARAM NAME="Command" VALUE="Close"></OBJECT>  
  91. <input type=button value=最小化 onclick=hh1.Click()>  
  92. <input type=button value=最大化 onclick=hh2.Click()>  
  93. <input type=button value=关闭 onclick=hh3.Click()>  
  94. 本例适用于IE  
  95.   
  96. 17.屏蔽功能键Shift,Alt,Ctrl  
  97. <script>  
  98. function look(){  
  99. if(event.shiftKey)  
  100. alert("禁止按Shift键!"); //可以换成ALT CTRL  
  101. }  
  102. document.onkeydown=look;  
  103. </script>  
  104.   
  105. 18. 网页不会被缓存  
  106. <META HTTP-EQUIV="pragma" CONTENT="no-cache">  
  107. <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">  
  108. <META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">  
  109. 或者<META HTTP-EQUIV="expires" CONTENT="0">  
  110.   
  111. 19.怎样让表单没有凹凸感?  
  112. <input type=text style="border:1 solid #000000">  
  113. 或  
  114. <input type=text style="border-left:none; border-right:none; border-top:none; border-bottom: 
  115.  
  116. 1 solid #000000"></textarea>  
  117.   
  118. 20.<div><span>&<layer>的区别?  
  119. <div>(division)用来定义大段的页面元素,会产生转行  
  120. <span>用来定义同一行内的元素,跟<div>的唯一区别是不产生转行  
  121. <layer>是ns的标记,ie不支持,相当于<div>  
  122.   
  123. 21.让弹出窗口总是在最上面:  
  124. <body onblur="this.focus();">  
  125.   
  126. 22.不要滚动条?  
  127. 让竖条没有:  
  128. <body style="overflow:scroll;overflow-y:hidden">  
  129. </body>  
  130. 让横条没有:  
  131. <body style="overflow:scroll;overflow-x:hidden">  
  132. </body>  
  133. 两个都去掉?更简单了  
  134. <body scroll="no">  
  135. </body>  
  136.   
  137. 23.怎样去掉图片链接点击后,图片周围的虚线?  
  138. <a href="#" onFocus="this.blur()"><img src="logo.jpg" border=0></a>  
  139.   
  140. 24.电子邮件处理提交表单  
  141. <form name="form1" method="post" action="mailto:****@***.com" enctype="text/plain">  
  142. <input type=submit>  
  143. </form>  
  144.   
  145. 25.在打开的子窗口刷新父窗口的代码里如何写?  
  146. window.opener.location.reload()  
  147.   
  148. 26.如何设定打开页面的大小  
  149. <body onload="top.resizeTo(300,200);">  
  150. 打开页面的位置<body onload="top.moveBy(300,200);">  
  151.   
  152. 27.在页面中如何加入不是满铺的背景图片,拉动页面时背景图不动  
  153. <STYLE>  
  154. body  
  155. {background-image:url(logo.gif); background-repeat:no-repeat;  
  156. background-position:center;background-attachment: fixed}  
  157. </STYLE>  
  158.   
  159. 28. 检查一段字符串是否全由数字组成  
  160. <script language="Javascript"><!--  
  161. function checkNum(str){return str.match(//D/)==null}  
  162. alert(checkNum("1232142141"))  
  163. alert(checkNum("123214214a1"))  
  164. // --></script>  
  165.   
  166. 29. 获得一个窗口的大小  
  167. document.body.clientWidth; document.body.clientHeight  
  168.   
  169. 30. 怎么判断是否是字符  
  170. if (/[^/x00-/xff]/g.test(s)) alert("含有汉字");  
  171. else alert("全是字符");  
  172.   
  173. 31.TEXTAREA自适应文字行数的多少  
  174. <textarea rows=1 name=s1 cols=27 onpropertychange=" this.style.posHeight=this.scrollHeight">  
  175. </textarea>  
  176. 32. 日期减去天数等于第二个日期  
  177. <script language=Javascript>  
  178. function cc(dd,dadd)  
  179. {  
  180. //可以加上错误处理  
  181. var a = new Date(dd)  
  182. a = a.valueOf()  
  183. a = a - dadd * 24 * 60 * 60 * 1000  
  184. a = new Date(a)  
  185. alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")  
  186. }  
  187. cc("12/23/2002",2)  
  188. </script>  
  189.   
  190. 33. 选择了哪一个Radio  
  191. <HTML><script language="vbscript">  
  192. function checkme()  
  193. for each ob in radio1  
  194. if ob.checked then window.alert ob.value  
  195. next  
  196. end function  
  197. </script><BODY>  
  198. <INPUT name="radio1" type="radio" value="style" checked>Style  
  199. <INPUT name="radio1" type="radio" value="barcode">Barcode  
  200. <INPUT type="button" value="check" onclick="checkme()">  
  201. </BODY></HTML>  
  202.   
  203. 34.脚本永不出错  
  204. <SCRIPT LANGUAGE="JavaScript">  
  205. <!-- Hide  
  206. function killErrors() {  
  207. return true;  
  208. }  
  209. window.onerror = killErrors;  
  210. // -->  
  211. </SCRIPT>  
  212.   
  213. 35.ENTER键可以让光标移到下一个输入框  
  214. <input onkeydown="if(event.keyCode==13)event.keyCode=9">  
  215.   
  216. 36. 检测某个网站的链接速度:  
  217. 把如下代码加入<body>区域中:  
  218. <script language=Javascript>  
  219. tim=1  
  220. setInterval("tim++",100)  
  221. b=1  
  222. var autourl=new Array()  
  223. autourl[1]="www.njcatv.net"  
  224. autourl[2]="javacool.3322.net"  
  225. autourl[3]=" www.sina.com.cn"  
  226. autourl[4]="www.nuaa.edu.cn"  
  227. autourl[5]="www.cctv.com"  
  228. function butt(){  
  229. document.write("<form name=autof>")  
  230. for(var i=1;i<autourl.length;i++)  
  231. document.write("<input type=text name=txt"+i+" size=10 value=测试中……> =》<input type=text 
  232. name=url"+i+" size=40> =》<input type=button value=GO 
  233.  
  234. onclick=window.open(this.form.url"+i+".value)><br>")  
  235. document.write("<input type=submit value=刷新></form>")  
  236. }  
  237. butt()  
  238. function auto(url){  
  239. document.forms[0]["url"+b].value=url  
  240. if(tim>200)  
  241. {document.forms[0]["txt"+b].value="链接超时"}  
  242. else  
  243. {document.forms[0]["txt"+b].value="时间"+tim/10+"秒"}  
  244. b++  
  245. }  
  246. function run(){for(var i=1;i<autourl.length;i++)document.write("<img src=http://"+autourl+"/"+Math.random()+" width=1 height=1 
  247.  
  248. onerror=auto("http://"+autourl+"")>")}  
  249. run()</script>  
  250.   
  251. 37. 各种样式的光标  
  252. auto :标准光标  
  253. default :标准箭头  
  254. hand :手形光标  
  255. wait :等待光标  
  256. text :I形光标  
  257. vertical-text :水平I形光标  
  258. no-drop :不可拖动光标  
  259. not-allowed :无效光标  
  260. help :?帮助光标  
  261. all-scroll :三角方向标  
  262. move :移动标  
  263. crosshair :十字标  
  264. e-resize  
  265. n-resize  
  266. nw-resize  
  267. w-resize  
  268. s-resize  
  269. se-resize  
  270. sw-resize  
  271.   
  272. 38.页面进入和退出的特效  
  273. 进入页面<meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)">  
  274. 推出页面<meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)">  
  275. 这个是页面被载入和调出时的一些特效。duration表示特效的持续时间,以秒为单位。transition表示使用哪种特效,取值为1-23:  
  276.   0 矩形缩小  
  277.   1 矩形扩大  
  278.   2 圆形缩小  
  279.   3 圆形扩大  
  280.   4 下到上刷新  
  281.   5 上到下刷新  
  282.   6 左到右刷新  
  283.   7 右到左刷新  
  284.   8 竖百叶窗  
  285.   9 横百叶窗  
  286.   10 错位横百叶窗  
  287.   11 错位竖百叶窗  
  288.   12 点扩散  
  289.   13 左右到中间刷新  
  290.   14 中间到左右刷新  
  291.   15 中间到上下  
  292.   16 上下到中间  
  293.   17 右下到左上  
  294.   18 右上到左下  
  295.   19 左上到右下  
  296.   20 左下到右上  
  297.   21 横条  
  298.   22 竖条  
  299.   23 以上22种随机选择一种  
  300.   
  301. 39.在规定时间内跳转  
  302. <META http-equiv=V="REFRESH" content="5;URL= http://www.51js.com">  
  303.   
  304. 40.网页是否被检索  
  305. <meta name="ROBOTS" content="属性值">  
  306.   其中属性值有以下一些:  
  307.   属性值为"all": 文件将被检索,且页上链接可被查询;  
  308.   属性值为"none": 文件不被检索,而且不查询页上的链接;  
  309.   属性值为"index": 文件将被检索;  
  310.   属性值为"follow": 查询页上的链接;  
  311.   属性值为"noindex": 文件不检索,但可被查询链接;  
  312.   属性值为"nofollow": 文件不被检索,但可查询页上的链接。  
  313.   
  314.   
  315. Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=245829  
   发表时间:2007-04-10  
好文章,都是很實用的網頁效果:)
0 请登录后投票
   发表时间:2007-04-10  
唉。。。好文章 它不是页得被评为 新手贴 隐藏帖么。。。
JavaEye的评论  根根本本就一点点都不公平
0 请登录后投票
   发表时间:2007-04-10  
真的很实用。
0 请登录后投票
   发表时间:2007-04-11  
好文章!直接收藏了,等着下次用!
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics