js例子

  1. //是text无法输入字符
  2. οnkeyup='this.value=this.value.replace(//D/gi,"")'
  3. //除去空格
  4. String.prototype.trim=function(){
  5.     return this.replace(/^/s+|/s$/g,"");
  6. }
  7. //历史后退
  8. window.history.back();
  9. //连接到地址
  10. window.location.href='';
  11. //中断执行
  12. setTimeout("alert()",1000);
  13.     //选中<select> 中的所有<option>
  14.     function setSelect(){
  15.         var opts = document.getElementById("selectTest").options;
  16.         for(var i = 0;i<opts.length;i++){
  17.             opts[i].selected = true;
  18.         }
  19.         return true;
  20.     }
  21.     
  22.     //获取<select>中选择的text
  23.     tForm.t_select.options[tForm.t_select.selectedIndex].text ;
  24.     
  25.     //插入一行数据
  26. function insertRow(tableid,arr){
  27.     var t = document.all(tableid);
  28.     var l = t.rows.length;
  29.     var row = t.insertRow(l);
  30.     for(var index in arr){
  31.         row.insertCell().innerText=arr[index];
  32.     }
  33. }
  34.     
  35. //--让打开本窗口的父页面重新刷新(在同一个窗口)-->
  36. location.replace(document.referrer);
  37. //--让打开本窗口的父窗口重新刷新(两个窗口)-->
  38. window.opener.location.reload();
  39. //<!--当前页面重新刷新-->
  40. location.reload(true);
  41. //当前窗口刷新。
  42. window.parent.location.reload();
  43.     
  44. 1. οncοntextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
  45. <table border οncοntextmenu=return(false)><td>no</table> 可用于Table
  46. 2. <body onselectstart="return false"> 取消选取、防止复制
  47. 3. οnpaste="return false" 不准粘贴
  48. 4. οncοpy="return false;" oncut="return false;" 防止复制
  49. 5. <link rel="Shortcut Icon" href="favicon.ico"> IE地址栏前换成自己的图标
  50. 6. <link rel="Bookmark" href="favicon.ico"> 可以在收藏夹中显示出你的图标
  51. 7. <input style="ime-mode:disabled"> 关闭输入法
  52. 8. 永远都会带着框架
  53. <script language="JavaScript"><!--
  54. if (window == top)top.location.href = "frames.htm"//frames.htm为框架网页
  55. // --></script>
  56. 9. 防止被人frame
  57. <SCRIPT LANGUAGE=JAVASCRIPT><!-- 
  58. if (top.location != self.location)top.location=self.location;
  59. // --></SCRIPT>
  60. 10. 网页将不能被另存为
  61. <noscript><iframe src="/blog/*.html>";</iframe></noscript> 
  62. 11. <input type=button value=查看网页源代码 
  63. οnclick="window.location = "view-source:"+ "http://www.williamlong.info"">
  64. 12.删除时确认
  65. <a href="javascript:if(confirm("确实要删除吗?"))location="boos.asp?&areyou=删除&page=1"">删除</a> 
  66. 13. 取得控件的绝对位置
  67. //Javascript
  68. <script language="Javascript">
  69. function getIE(e){
  70. var t=e.offsetTop;
  71. var l=e.offsetLeft;
  72. while(e=e.offsetParent)
  73. alert("top="+t+"/nleft="+l);
  74. }
  75. </script>
  76. //VBScript
  77. <script language="VBScript"><!--
  78. function getIE()
  79. dim t,l,a,b
  80. set a=document.all.img1
  81. t=document.all.img1.offsetTop
  82. l=document.all.img1.offsetLeft
  83. while a.tagName<>"BODY"
  84. set a = a.offsetParent
  85. t=t+a.offsetTop
  86. l=l+a.offsetLeft
  87. wend
  88. msgbox "top="&t&chr(13)&"left="&l,64,"得到控件的位置"
  89. end function
  90. --></script>
  91. 14. 光标是停在文本框文字的最后
  92. <script language="javascript">
  93. function cc()
  94. {
  95. var e = event.srcElement;
  96. var r =e.createTextRange();
  97. r.moveStart("character",e.value.length);
  98. r.collapse(true);
  99. r.select();
  100. }
  101. </script>
  102. <input type=text name=text1 value="123" οnfοcus="cc()">
  103. 15. 判断上一页的来源
  104. javascript:
  105. document.referrer
  106. 16. 最小化、最大化、关闭窗口
  107. <object id=hh1 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11"
  108. <param name="Command" value="Minimize"></object>
  109. <object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11"
  110. <param name="Command" value="Maximize"></object>
  111. <OBJECT id=hh3 classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
  112. <PARAM NAME="Command" VALUE="Close"></OBJECT>
  113. <input type=button value=最小化 οnclick=hh1.Click()>
  114. <input type=button value=最大化 οnclick=hh2.Click()>
  115. <input type=button value=关闭 οnclick=hh3.Click()>
  116. 本例适用于IE
  117. 17.屏蔽功能键Shift,Alt,Ctrl
  118. <script>
  119. function look(){ 
  120. if(event.shiftKey) 
  121. alert("禁止按Shift键!"); //可以换成ALT CTRL
  122. document.οnkeydοwn=look; 
  123. </script>
  124. 18. 网页不会被缓存
  125. <META HTTP-EQUIV="pragma" CONTENT="no-cache">
  126. <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
  127. <META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
  128. 或者<META HTTP-EQUIV="expires" CONTENT="0">
  129. 19.怎样让表单没有凹凸感?
  130. <input type=text style="border:1 solid #000000"
  131. <input type=text style="border-left:none; border-right:none; border-top:none; border-bottom: 1 solid #000000"></textarea>
  132. 20.<div><span>&<layer>的区别? 
  133. <div>(division)用来定义大段的页面元素,会产生转行 
  134. <span>用来定义同一行内的元素,跟<div>的唯一区别是不产生转行 
  135. <layer>是ns的标记,ie不支持,相当于<div>
  136. 21.让弹出窗口总是在最上面:
  137. <body οnblur="this.focus();">
  138. 22.不要滚动条? 
  139. 让竖条没有: 
  140. <body style="overflow:scroll;overflow-y:hidden"
  141. </body> 
  142. 让横条没有: 
  143. <body style="overflow:scroll;overflow-x:hidden"
  144. </body> 
  145. 两个都去掉?更简单了 
  146. <body scroll="no"
  147. </body> 
  148. 23.怎样去掉图片链接点击后,图片周围的虚线?
  149. <a href="#" onFocus="this.blur()"><img src="/blog/logo.jpg" border=0></a>
  150. 24.电子邮件处理提交表单
  151. <form name="form1" method="post" action="mailto:****@***.com" enctype="text/plain"
  152. <input type=submit>
  153. </form>
  154. 25.在打开的子窗口刷新父窗口的代码里如何写?
  155. window.opener.location.reload()
  156. 26.如何设定打开页面的大小
  157. <body οnlοad="top.resizeTo(300,200);">
  158. 打开页面的位置<body οnlοad="top.moveBy(300,200);">
  159. 27.在页面中如何加入不是满铺的背景图片,拉动页面时背景图不动 
  160. <STYLE> 
  161. body 
  162. {background-image:url(/blog/logo.gif); background-repeat:no-repeat; 
  163. background-position:center;background-attachment: fixed
  164. </STYLE> 
  165. 28. 检查一段字符串是否全由数字组成
  166. <script language="Javascript"><!--
  167. function checkNum(str){return str.match(//D/)==null}
  168. alert(checkNum("1232142141"))
  169. alert(checkNum("123214214a1"))
  170. // --></script>
  171. 29. 获得一个窗口的大小
  172. document.body.clientWidth; document.body.clientHeight
  173. 30. 怎么判断是否是字符
  174. if (/[^/x00-/xff]/g.test(s)) alert("含有汉字");
  175. else alert("全是字符");
  176. 31.TEXTAREA自适应文字行数的多少
  177. <textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.scrollHeight">
  178. </textarea>
  179. 32. 日期减去天数等于第二个日期
  180. <script language=Javascript>
  181. function cc(dd,dadd)
  182. {
  183. //可以加上错误处理
  184. var a = new Date(dd)
  185. a = a.valueOf()
  186. a = a - dadd * 24 * 60 * 60 * 1000
  187. a = new Date(a)
  188. alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
  189. }
  190. cc("12/23/2002",2)
  191. </script>
  192. 33. 选择了哪一个Radio
  193. <HTML><script language="vbscript">
  194. function checkme()
  195. for each ob in radio1
  196. if ob.checked then window.alert ob.value
  197. next
  198. end function
  199. </script><BODY>
  200. <INPUT name="radio1" type="radio" value="style" checked>Style
  201. <INPUT name="radio1" type="radio" value="barcode">Barcode
  202. <INPUT type="button" value="check" οnclick="checkme()">
  203. </BODY></HTML>
  204. 34.脚本永不出错
  205. <SCRIPT LANGUAGE="JavaScript"
  206. <!-- Hide 
  207. function killErrors() { 
  208. return true
  209. window.onerror = killErrors; 
  210. // --> 
  211. </SCRIPT>
  212. 35.ENTER键可以让光标移到下一个输入框
  213. <input οnkeydοwn="if(event.keyCode==13)event.keyCode=9">
  214. 36. 检测某个网站的链接速度:
  215. 把如下代码加入<body>区域中:
  216. <script language=Javascript>
  217. tim=1
  218. setInterval("tim++",100)
  219. b=1
  220. var autourl=new Array()
  221. autourl[1]="www.njcatv.net"
  222. autourl[2]="javacool.3322.net"
  223. autourl[3]="www.sina.com.cn"
  224. autourl[4]="www.nuaa.edu.cn"
  225. autourl[5]="www.cctv.com"
  226. function butt(){
  227. document.write("<form name=autof>")
  228. for(var i=1;i<autourl.length;i++)
  229. document.write("<input type=text name=txt"+i+" size=10 value="/blog/测试中......>" =》<input type=text 
  230. name=url"+i+" size=40> =》<input type=button value=GO 
  231. οnclick=window.open(this.form.url"+i+".value)><br/>")
  232. document.write("<input type=submit value=刷新></form>")
  233. }
  234. butt()
  235. function auto(url)
  236. else
  237. b++
  238. }
  239. function run(){for(var i=1;i<autourl.length;i++)document.write("<img src=http://"+autourl+"/"+Math.random()+" width=1 height=1 
  240. οnerrοr=auto("http://"+autourl+"")>")}
  241. run()</script>
  242. 37. 各种样式的光标
  243. auto :标准光标
  244. default :标准箭头
  245. hand :手形光标
  246. wait :等待光标
  247. text :I形光标
  248. vertical-text :水平I形光标
  249. no-drop :不可拖动光标
  250. not-allowed :无效光标
  251. help :?帮助光标
  252. all-scroll :三角方向标
  253. move :移动标
  254. crosshair :十字标
  255. e-resize
  256. n-resize
  257. nw-resize
  258. w-resize
  259. s-resize
  260. se-resize
  261. sw-resize
  262. 38.页面进入和退出的特效
  263. 进入页面<meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)">
  264. 推出页面<meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)">  
  265. 这个是页面被载入和调出时的一些特效。duration表示特效的持续时间,以秒为单位。transition表示使用哪种特效,取值为1-23:
  266.   0 矩形缩小 
  267.   1 矩形扩大 
  268.   2 圆形缩小
  269.   3 圆形扩大 
  270.   4 下到上刷新 
  271.   5 上到下刷新
  272.   6 左到右刷新 
  273.   7 右到左刷新 
  274.   8 竖百叶窗
  275.   9 横百叶窗 
  276.   10 错位横百叶窗 
  277.   11 错位竖百叶窗
  278.   12 点扩散 
  279.   13 左右到中间刷新 
  280.   14 中间到左右刷新
  281.   15 中间到上下
  282.   16 上下到中间 
  283.   17 右下到左上
  284.   18 右上到左下 
  285.   19 左上到右下 
  286.   20 左下到右上
  287.   21 横条 
  288.   22 竖条 
  289.   23 以上22种随机选择一种
  290. 39.在规定时间内跳转
  291. <META http-equiv=V="REFRESH" content="5;URL=http://www.williamlong.info"
  292. 40.网页是否被检索
  293. <meta name="ROBOTS" content="属性值">
  294.   其中属性值有以下一些:
  295.   属性值为"all": 文件将被检索,且页上链接可被查询;
  296.   属性值为"none": 文件不被检索,而且不查询页上的链接;
  297.   属性值为"index": 文件将被检索;
  298.   属性值为"follow": 查询页上的链接;
  299.   属性值为"noindex": 文件不检索,但可被查询链接;
  300.   属性值为"nofollow": 文件不被检索,但可查询页上的链接。
  301. 最大化窗口?
  302. <script language="JavaScript">
  303. <!--
  304. self.moveTo(0,0)
  305. self.resizeTo(screen.availWidth,screen.availHeight)
  306. //-->
  307. </script>
  308.  
  309. 解决问题:由于层与下拉框之间的优先级是:下拉框 > 层,因此在显示的时候,会因为优先级的次序而会出现如上问题。(如果几个元素都是层的话,我们可以通过层的 z-index 属性来设置)解决办法就是:给层中放一个优先级比下拉框更高的元素(iframe),从而解决此问题!具体解决代码如下:
  310. <div id="menu" style="position:absolute; visibility:hidden; top:20px; left:20px; width:100px; height:200px; background-color:#6699cc;">
  311. <table>
  312.   <tr><td>item 1</td></tr>
  313.   <tr><td>item 2</td></tr>
  314.   <tr><td>item 3</td></tr>
  315.   <tr><td>item 4</td></tr>
  316.   <tr><td>item 5</td></tr>
  317.   </table>
  318.   <iframe src="/blog/javascript:false" style="position:absolute; visibility:inherit; top:0px; left:0px; width:100px; height:200px; z-index:-1; filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';"></iframe>
  319. </div>
  320. <a href="#" οnclick="document.getElementById('menu').style.visibility='visible'">menu</a>
  321. <form>
  322.   <select><option>A form selection list</option></select>
  323. </form>
  324. 输入框也可以做的很漂亮了
  325. <div align="center"><input type="hidden" name="hao" value="yes">
  326.               外向数:<input 
  327.       name=answer 
  328.       style="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)">
  329.              没回答的题数:<input 
  330.       name=unanswer id="unanswer" 
  331.       style="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)">
  332.             <br/>
  333.             总得分:
  334.             <input 
  335.       name=score id="score" 
  336.       style="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)">
  337.              结    论:
  338.             <input 
  339.       name=xgjg id="xgjg" 
  340.       style="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)">
  341.             <br/>
  342.             <br/> 
  343.             
  344.             <input onClick=processForm(this.form) style="FONT-FAMILY: 宋体; FONT-SIZE: 9pt" type=button value=查看结果 name="button"
  345.              <input type="reset" name="Submit" value="重做">
  346.             </div>
  347. 注意:修改<body>为<body οnlοad="max.Click()">即为打开最大
  348. 化窗口,而如果改为<body οnlοad="min.Click()">就变为窗口一打开就最小化
  349. <object id="min" type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
  350.             <param name="Command" value="Minimize">
  351.           </object> <object id="max" type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
  352.             <param name="Command" value="Maximize">
  353.           </object>
  354. </body>
  355. 页面自动刷新(说明)
  356. 当你做网页时,是不是有的时候想让你的网页自动不停刷新,或者过一段时间自动跳转到另外一个你自己设定的页面?其实实现这个效果非常地简单,而且这个效果甚至不能称之为特效。你只要把如下代码加入你的网页中就可以了。
  357. 1,页面自动刷新:把如下代码加入<head>区域中<meta http-equiv="refresh" content="20">,其中20指每隔20秒刷新一次页面.
  358. 2,页面自动跳转:把如下代码加入<head>区域中<meta http-equiv="refresh" content="20;url=http://www.williamlong.info">,其中20指隔20秒后跳转到http://www.williamlong.info页面。 
  359. 页面自动关闭
  360. 5000是指时间<body onLoad="setTimeout(window.close, 5000)"
  361. 弹出窗口自动关闭 
  362. 10秒后弹出窗口自动关闭
  363. 注意:在新的tan.htm的body中要加 <onLoad="closeit()">
  364. head
  365. <script language="JavaScript">
  366. <!--
  367. var gt = unescape('%3e');
  368. var popup = null;
  369. var over = "Launch Pop-up Navigator";
  370. popup = window.open('''popupnav''width=225,height=235,resizable=1,scrollbars=auto');
  371. if (popup != null) {
  372. if (popup.opener == null) {
  373. popup.opener = self; 
  374. }
  375. popup.location.href = 'tan.htm';
  376. }
  377. // -->
  378. </script>
  379. <body>注意:这段代码是在新建文件中的
  380. <script language="JavaScript">
  381.  function closeit() 
  382.  </script>
  383. 这个可不是<iframe>(引用)呀。是直接调用的。以下代码加入<body>区域
  384. <object type="text/x-scriptlet" width="800"  height="1000" data="../index.htm">
  385. </object>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值