js学习

js 代码
  1. 10.关闭输入法   
  2.   
  3.  < input style="ime-mode:disabled">   
  4.   
  5.   
  6.   
  7. 11.直接查看源代码   
  8.     
  9.  < input type=button value=查看网页源代码 οnclick="window.location = 'view-source:'+ 'http://www.csdn.net/'">   
  10.   
  11.   
  12.   
  13. 12.在Javascript中定义一个对象(属性,方法)   
  14.   
  15. function pasta(grain, width, hasEgg) {   
  16.     this.grain = grain;   
  17.     this.width = width;   
  18.     this.hasEgg = hasEgg;   
  19.     this.toString = pastaToString;   
  20. }   
  21.   
  22. function pastaToString() {   
  23.     return "Grain: " + this.grain + "\n" + "Width: " + this.width + "\n" + "Egg?: " + Boolean(this.hasEgg);   
  24. }   
  25.   
  26. var P1=new pasta(3,3,false);   
  27.   
  28.   
  29.   
  30. 13. 取得控件的绝对位置   
  31.   
  32. //Javascript   
  33. < script language="Javascript">   
  34. function getIE(e){   
  35.    var t=e.offsetTop;   
  36.    var l=e.offsetLeft;   
  37.    while(e=e.offsetParent){   
  38.       t+=e.offsetTop;   
  39.       l+=e.offsetLeft;   
  40.    }   
  41.    alert("top="+t+"\nleft="+l);   
  42. }   
  43. < /script>   
  44.   
  45.   
  46.   
  47. 14. 光标是停在文本框文字的最后   
  48.   
  49. < script language="javascript">   
  50. function cc()   
  51. {   
  52.    var e = event.srcElement;   
  53.    var r =e.createTextRange();   
  54.    r.moveStart('character',e.value.length);   
  55.    r.collapse(true);   
  56.    r.select();   
  57. }   
  58. < /script>   
  59. < input type=text name=text1 value="123" οnfοcus="cc()">   
  60.   
  61.   
  62.   
  63. 15. 判断上一页的来源   
  64.   
  65. asp:   
  66.   request.servervariables("HTTP_REFERER")   
  67.   
  68. javascript:    
  69.   document.referrer   
  70.   
  71.   
  72.   
  73. 16. 最小化、最大化、关闭窗口   
  74.   
  75. < object id=hh1 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">   
  76. < param name="Command" value="Minimize">< /object>   
  77. < object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">   
  78. < param name="Command" value="Maximize">< /object>   
  79. < OBJECT id=hh3 classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">   
  80. < PARAM NAME="Command" VALUE="Close">< /OBJECT>   
  81.   
  82. < input type=button value=最小化 οnclick=hh1.Click()>   
  83. < input type=button value=最大化 οnclick=hh2.Click()>   
  84. < input type=button value=关闭 οnclick=hh3.Click()>   
  85. 本例适用于IE   
  86.   
  87.   
  88.   
  89. 17. 检查一段字符串是否全由数字组成   
  90.   
  91. < script language="Javascript">< !--   
  92. function checkNum(str){return str.match(/\D/)==null}   
  93. // -->< /script>   
  94.   
  95.   
  96.   
  97. 18. 获得一个窗口的大小   
  98.   
  99. document.body.clientWidth,document.body.clientHeight   
  100.   
  101.   
  102.   
  103. 19. 怎么判断是否是字符   
  104.   
  105. if (/[^\x00-\xff]/g.test(s))   
  106.   alert("含有汉字");   
  107. else  
  108.   alert("全是字符");   
  109.   
  110.   
  111.   
  112. 20. 检测某个网站的链接速度   
  113.   
  114. 把如下代码加入< body>区域中:   
  115. < script language=Javascript>   
  116. tim=1   
  117. setInterval("tim++",100)   
  118. b=1   
  119.   
  120. var autourl=new Array()   
  121. autourl[1]="www.njcatv.net"  
  122. autourl[2]="javacool.3322.net"  
  123. autourl[3]="www.sina.com.cn"  
  124. autourl[4]="www.nuaa.edu.cn"  
  125. autourl[5]="www.cctv.com"  
  126.   
  127. function butt(){   
  128.    document.write("< form name=autof>")   
  129.    for(var i=1;i< autourl.length;i++)   
  130.       document.write("< input type=text name=txt"+i+" size=10 value=测试中……> =》< input type=text name=url"+i+" size=40> =》< input type=button value=GO οnclick=window.open(this.form.url"+i+".value)>< br/>")   
  131.    document.write("< input type=submit value=刷新>< /form>")   
  132. }   
  133. butt()   
  134. function auto(url){   
  135.    document.forms[0]["url"+b].value=url   
  136.    if(tim>200)   
  137.    {   
  138.       document.forms[0]["txt"+b].value="链接超时"  
  139.    }   
  140.    else  
  141.    {   
  142.       document.forms[0]["txt"+b].value="时间"+tim/10+"秒"  
  143.    }   
  144.    b++   
  145. }   
  146. function run()   
  147. {   
  148.    for(var i=1;i< autourl.length;i++)   
  149.       document.write("< img src=http://"+autourl[i]+"/"+Math.random()+" width=1 height=1 οnerrοr=auto('http://"+autourl[i]+"')>")   
  150. }   
  151. run()< /script>   
  152.   
  153.   
  154.   
  155. 21. 各种样式的光标   
  156.   
  157. auto :标准光标   
  158. default :标准箭头   
  159. hand :手形光标   
  160. wait :等待光标   
  161. text :I形光标   
  162. vertical-text :水平I形光标   
  163. no-drop :不可拖动光标   
  164. not-allowed :无效光标   
  165. help :?帮助光标   
  166. all-scroll :三角方向标   
  167. move :移动标   
  168. crosshair :十字标   
  169. e-resize   
  170. n-resize   
  171. nw-resize   
  172. w-resize   
  173. s-resize   
  174. se-resize   
  175. sw-resize   
  176.   
  177.   
  178.   
  179. 22.让TEXTAREA自适应文字的行数   
  180.   
  181. < textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.scrollHeight">< /textarea>   
  182.   
  183.   
  184.   
  185. 23. 日期减去天数等于第二个日期   
  186. < script language=Javascript>   
  187. function cc(dd,dadd)   
  188. {   
  189. //可以加上错误处理   
  190.    var a = new Date(dd)   
  191.    a = a.valueOf()   
  192.    a = a - dadd * 24 * 60 * 60 * 1000   
  193.    a = new Date(a)   
  194.    alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")   
  195. }   
  196. cc("12/23/2002",2)   
  197. < /script>   
  198.   
  199.   
  200.   
  201. 24. 选择了哪一个Radio   
  202.   
  203. < HTML>   
  204. < script language="vbscript">   
  205. function checkme()   
  206.    for each ob in radio1   
  207.    if ob.checked then   
  208.       window.alert ob.value   
  209.    next   
  210. end function  
  211. < /script>   
  212. < BODY>   
  213. < INPUT name="radio1" type="radio" value="style" checked>Style   
  214. < INPUT name="radio1" type="radio" value="barcode">Barcode   
  215. < INPUT type="button" value="check" οnclick="checkme()">   
  216. < /BODY>< /HTML>   
  217.   
  218.   
  219.   
  220. 25.获得本页url的request.servervariables("")集合   
  221.   
  222. Response.Write "< TABLE border=1>< !-- Table Header -->< TR>< TD>< B>Variables< /B>< /TD>< TD>< B>Value< /B>< /TD>< /TR>"  
  223. for each ob in Request.ServerVariables   
  224.    Response.Write "< TR>< TD>"&ob&"< /TD>< TD>"&Request.ServerVariables(ob)&"< /TD>< /TR>"  
  225. next   
  226. Response.Write "< /TABLE>"  
  227.   
  228.   
  229.   
  230. 26.ENTER键可以让光标移到下一个输入框   
  231.   
  232. < input οnkeydοwn="if(event.keyCode==13)event.keyCode=9">   
  233.   
  234.   
  235.   
  236. 28.引用其他网页   
  237.   
  238. < table width=100% border="0">< tr>< td>< script language="JavaScript" location="http://91down.7161.net" id="nd91down" src="http://91down.7161.net/js/new1-1.htm">< /script>< /td>< td>< script language="JavaScript" location="http://91down.7161.net" id="nd91down" src="http://91down.7161.net/js/new1-2.htm">< /script>< /td>< /tr>< /table>   
  239.   
  240.   
  241.   
  242. 29.常用的正则表达式   
  243.   
  244. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]   
  245. 匹配双字节字符(包括汉字在内):[^\x00-\xff]   
  246. 匹配空行的正则表达式:\n[\s| ]*\r   
  247. 匹配HTML标记的正则表达式:/< (.*)>.*< \/\1>|< (.*) \/>/   
  248. 匹配首尾空格的正则表达式:(^\s*)|(\s*$)   
  249. 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*   
  250. 匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?   
  251.   
  252. (1)应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)           
  253.     String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}   
  254.   
  255. (2)应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现   
  256.    String.prototype.trim = function()   
  257.    {   
  258.       return this.replace(/(^\s*)|(\s*$)/g, "");   
  259.    }   
  260. (3)应用:利用正则表达式分解和转换IP地址   
  261.    function IP2V(ip) //IP地址转换成对应数值   
  262.    {   
  263.       re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式   
  264.       if(re.test(ip))   
  265.       {   
  266.          return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1   
  267.       }   
  268.       else  
  269.       {   
  270.          throw new Error("Not a valid IP address!")   
  271.       }   
  272.    }   
  273. (4)应用:从URL地址中提取文件名的javascript程序   
  274.    s="http://www.9499.net/page1.htm";   
  275.    s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2") ; //Page1.htm   
  276. (5)应用:利用正则表达式限制网页表单里的文本框输入内容   
  277.    用正则表达式限制只能输入中文:οnkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"  
  278.    用正则表达式限制只能输入全角字符: οnkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"  
  279.    用正则表达式限制只能输入数字:οnkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"  
  280.    用正则表达式限制只能输入数字和英文:οnkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"  
  281.   
  282.   
  283.   
  284. 30.弹出来提示对话框   
  285. window.showModalDialog(url);   
  286.   
  287.   
  288.   
  289. 31.取得查询字符串,然后就生成成对的数组   
  290. var argstr = window.top.location.search.substring(1,window.top.location.search.length);   
  291. var args = argstr.split('&');   
  292.   
  293.   
  294.   
  295. 32.另类的onload函数   
  296. < SCRIPT FOR=window event=onload>   
  297. try  
  298. {   
  299.   Start();   
  300. }   
  301. catch (exception)   
  302. {   
  303. }   
  304. < /script>   
  305.   
  306.   
  307.   
  308. 33.取得IE的版本   
  309. var ieVer = parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf("MSIE ")+5, navigator.appVersion.length))   
  310. var isIE6 = ieVer >= 6.0   
  311.   
  312.   
  313.   
  314. 34.提交表单   
  315. // aimForm 为表单名    aimPage为提交到的页   
  316.   
  317. //提交表单到新建的网页   
  318. function SubmitFormToNewPage(aimForm,aimPage){   
  319.    aimForm.method="POST";   
  320.    aimForm.target="_blank";   
  321.    aimForm.action=aimPage;   
  322.    aimForm.submit();   
  323. }   
  324.   
  325. //在本地提交表单   
  326. function SubmitFormToLocalPage(aimForm,aimPage){   
  327.    aimForm.method="POST";   
  328.    aimForm.target="_self";   
  329.    aimForm.action=aimPage;   
  330.    aimForm.submit();   
  331. }   
  332.   
  333.   
  334.   
  335.   
  336. 35.判断是否是整数   
  337. function IsNum(s)  //整数   
  338. {   
  339.    if(s=="null"||s=="undefined"||s.length< 1)   
  340.       return false;   
  341.    if(isNaN(parseInt(s)))   
  342.       return false;   
  343.    else  
  344.    if((parseInt(s)+"").length!=s.length)   
  345.       return false;   
  346.    else  
  347.       return true;   
  348. }   
  349.   
  350. function IsNumber(JudgeNum){  //判断大于0的数   
  351.    var JudgeStr=JudgeNum.toString();   
  352.    for (var i=0;i< JudgeStr.length;i++) {   
  353.       var oneChar=JudgeStr.charAt(i);   
  354.   
  355.       if (oneChar< "0" || oneChar >"9"){   
  356.          return false;   
  357.       }   
  358.    }   
  359.    return true;   
  360. }   
  361.   
  362.   
  363.   
  364. 36.链接css文件和js文件   
  365. < link rel="stylesheet" href="../css/style.css" type="text/css">   
  366. < script language="javascript" src="../includes/jslib.js" >< /script>   
  367.   
  368.   
  369.   
  370. 37.引用框架的内容   
  371. window.top.frames["mainFrame"].location.href=s;   
  372. 在IFRAME标签中引用框架的内容   
  373. parent.frames["mainFrame"].location.href   
  374. 在窗口中引用IFrame中的内容   
  375. window.top.frames["mainFrame"].confFrame.location.href   
  376.   
  377.   
  378.   
  379. 38.打开没有最大化按钮的窗口   
  380. window.open("http://www.google.com","","width=250,height=220,scrollbars=no,resizable=no,center=yes");   
  381.   
  382.   
  383.   
  384. 39.在页面上显示一个有边框的Frame   
  385. < fieldset style="width:500;height:100">   
  386. < legend>标题< /legend>   
  387. 具体内容   
  388. < /fieldset>   
  389.   
  390.   
  391.   
  392. 40.判断日期1是不是大于日期2   
  393.   
  394. function IsDate1AfterThanDate2(year1,month1,day1,year2,month2,day2){   
  395.    var iFrom=Date.parse(month1+"-"+day1+"-"+year1);   
  396.    var iTo=Date.parse(month2+"-"+day2+"-"+year2);   
  397.    if(iFrom>iTo)   
  398.       return true;   
  399.    else  
  400.       return false;   
  401. }   
  402.   
  403. function IsDate(year,month,day){   
  404.    if( (!IsNumber(year))||(year.length!=4))   
  405.       return false;   
  406.    if( (!IsNumber(month))||(month>12)||(month< 1) )   
  407.       return false;   
  408.    if( (!IsNumber(day))||(day>31)||(day< 1) )   
  409.       return false;   
  410.   
  411.    var myDate=new Date();   
  412.       myDate.setFullYear(year,month-1,day);   
  413.    if (isNaN(myDate.getFullYear())){   
  414.       return false;   
  415.    }   
  416.    else{   
  417.       if( (myDate.getFullYear()!=year)||(myDate.getDate()!=day)||(myDate.getMonth()!=(month-1).toString()) )   
  418.          return false;   
  419.    }   
  420.       return true;   
  421. }   
  422.   
  423. function IsNumber(JudgeNum){   
  424.    var JudgeStr=JudgeNum.toString();   
  425.    for (var i=0;i< JudgeStr.length;i++) {   
  426.       var oneChar=JudgeStr.charAt(i);   
  427.   
  428.       if (oneChar< "0" || oneChar >"9"){   
  429.          return false;   
  430.       }   
  431.    }   
  432.    return true;   
  433. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值