JavaScript函数小结
JS基础知识
1 /********************** 2 1:基础知识 3 4 1 创建脚本块 5 6 1: <script language=”JavaScript”> 7 2: JavaScript code goes here 8 3: </script> 9 10 2 隐藏脚本代码 11 12 1: <script language=”JavaScript”> 13 2: <!-- 14 3: document.write(“Hello”); 15 4: // --> 16 5: </script> 17 18 在不支持JavaScript的浏览器中将不执行相关代码 19 20 3 浏览器不支持的时候显示 21 22 1: <noscript> 23 2: Hello to the non-JavaScript browser. 24 3: </noscript> 25 26 4 链接外部脚本文件 27 28 1: <script language=”JavaScript” src="/”filename.js"”></script> 29 30 5 注释脚本 31 32 1: // This is a comment 33 2: document.write(“Hello”); // This is a comment 34 3: /* 35 4: All of this 36 5: is a comment 37 6: */ 38 39 6 输出到浏览器 40 41 1: document.write(“<strong>Hello</strong>”); 42 43 7 定义变量 44 45 1: var myVariable = “some value”; 46 47 8 字符串相加 48 49 1: var myString = “String1” + “String2”; 50 51 9 字符串搜索 52 53 1: <script language=”JavaScript”> 54 2: <!-- 55 3: var myVariable = “Hello there”; 56 4: var therePlace = myVariable.search(“there”); 57 5: document.write(therePlace); 58 6: // --> 59 7: </script> 60 61 10 字符串替换 62 63 1: thisVar.replace(“Monday”,”Friday”); 64 65 11 格式化字串 66 67 1: <script language=”JavaScript”> 68 2: <!-- 69 3: var myVariable = “Hello there”; 70 4: document.write(myVariable.big() + “<br/>”); 71 5: document.write(myVariable.blink() + “<br/>”); 72 6: document.write(myVariable.bold() + “<br/>”); 73 7: document.write(myVariable.fixed() + “<br/>”); 74 8: document.write(myVariable.fontcolor(“red”) + “<br/>”); 75 9: document.write(myVariable.fontsize(“18pt”) + “<br/>”); 76 10: document.write(myVariable.italics() + “<br/>”); 77 11: document.write(myVariable.small() + “<br/>”); 78 12: document.write(myVariable.strike() + “<br/>”); 79 13: document.write(myVariable.sub() + “<br/>”); 80 14: document.write(myVariable.sup() + “<br/>”); 81 15: document.write(myVariable.toLowerCase() + “<br/>”); 82 16: document.write(myVariable.toUpperCase() + “<br/>”); 83 17: 84 18: var firstString = “My String”; 85 19: var finalString = firstString.bold().toLowerCase().fontcolor(“red”); 86 20: // --> 87 21: </script> 88 89 12 创建数组 90 91 1: <script language=”JavaScript”> 92 2: <!-- 93 3: var myArray = new Array(5); 94 4: myArray[0] = “First Entry”; 95 5: myArray[1] = “Second Entry”; 96 6: myArray[2] = “Third Entry”; 97 7: myArray[3] = “Fourth Entry”; 98 8: myArray[4] = “Fifth Entry”; 99 9: var anotherArray = new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”); 100 10: // --> 101 11: </script> 102 103 13 数组排序 104 105 1: <script language=”JavaScript”> 106 2: <!-- 107 3: var myArray = new Array(5); 108 4: myArray[0] = “z”; 109 5: myArray[1] = “c”; 110 6: myArray[2] = “d”; 111 7: myArray[3] = “a”; 112 8: myArray[4] = “q”; 113 9: document.write(myArray.sort()); 114 10: // --> 115 11: </script> 116 117 14 分割字符串 118 119 1: <script language=”JavaScript”> 120 2: <!-- 121 3: var myVariable = “a,b,c,d”; 122 4: var stringArray = myVariable.split(“,”); 123 5: document.write(stringArray[0]); 124 6: document.write(stringArray[1]); 125 7: document.write(stringArray[2]); 126 8: document.write(stringArray[3]); 127 9: // --> 128 10: </script> 129 130 15 弹出警告信息 131 132 1: <script language=”JavaScript”> 133 2: <!-- 134 3: window.alert(“Hello”); 135 4: // --> 136 5: </script> 137 138 16 弹出确认框 139 140 1: <script language=”JavaScript”> 141 2: <!-- 142 3: var result = window.confirm(“Click OK to continue”); 143 4: // --> 144 5: </script> 145 146 17 定义函数 147 148 1: <script language=”JavaScript”> 149 2: <!-- 150 3: function multiple(number1,number2) { 151 4: var result = number1 * number2; 152 5: return result; 153 6: } 154 7: // --> 155 8: </script> 156 157 18 调用JS函数 158 159 1: <a href=”#” onClick=”functionName()”>Link text</a> 160 2: <a href="/”javascript:functionName"()”>Link text</a> 161 162 19 在页面加载完成后执行函数 163 164 1: <body onLoad=”functionName();”> 165 2: Body of the page 166 3: </body> 167 168 20 条件判断 169 170 1: <script> 171 2: <!-- 172 3: var userChoice = window.confirm(“Choose OK or Cancel”); 173 4: var result = (userChoice == true) ? “OK” : “Cancel”; 174 5: document.write(result); 175 6: // --> 176 7: </script> 177 21 指定次数循环 178 179 1: <script> 180 2: <!-- 181 3: var myArray = new Array(3); 182 4: myArray[0] = “Item 0”; 183 5: myArray[1] = “Item 1”; 184 6: myArray[2] = “Item 2”; 185 7: for (i = 0; i < myArray.length; i++) { 186 8: document.write(myArray[i] + “<br/>”); 187 9: } 188 10: // --> 189 11: </script> 190 191 22 设定将来执行 192 193 1: <script> 194 2: <!-- 195 3: function hello() { 196 4: window.alert(“Hello”); 197 5: } 198 6: window.setTimeout(“hello()”,5000); 199 7: // --> 200 8: </script> 201 202 23 定时执行函数 203 204 1: <script> 205 2: <!-- 206 3: function hello() { 207 4: window.alert(“Hello”); 208 5: window.setTimeout(“hello()”,5000); 209 6: } 210 7: window.setTimeout(“hello()”,5000); 211 8: // --> 212 9: </script> 213 214 24 取消定时执行 215 216 1: <script> 217 2: <!-- 218 3: function hello() { 219 4: window.alert(“Hello”); 220 5: } 221 6: var myTimeout = window.setTimeout(“hello()”,5000); 222 7: window.clearTimeout(myTimeout); 223 8: // --> 224 9: </script> 225 226 25 在页面卸载时候执行函数 227 228 1: <body onUnload=”functionName();”> 229 2: Body of the page 230 3: </body> 231 232 JavaScript就这么回事2:浏览器输出 233 234 26 访问document对象 235 236 1: <script language=”JavaScript”> 237 2: var myURL = document.URL; 238 3: window.alert(myURL); 239 4: </script> 240 241 27 动态输出HTML 242 243 1: <script language=”JavaScript”> 244 2: document.write(“<p>Here’s some information about this document:</p>”); 245 3: document.write(“<ul>”); 246 4: document.write(“<li>Referring Document: “ + document.referrer + “</li>”); 247 5: document.write(“<li>Domain: “ + document.domain + “</li>”); 248 6: document.write(“<li>URL: “ + document.URL + “</li>”); 249 7: document.write(“</ul>”); 250 8: </script> 251 252 28 输出换行 253 254 1: document.writeln(“<strong>a</strong>”); 255 2: document.writeln(“b”); 256 257 29 输出日期 258 259 1: <script language=”JavaScript”> 260 2: var thisDate = new Date(); 261 3: document.write(thisDate.toString()); 262 4: </script> 263 264 30 指定日期的时区 265 266 1: <script language=”JavaScript”> 267 2: var myOffset = -2; 268 3: var currentDate = new Date(); 269 4: var userOffset = currentDate.getTimezoneOffset()/60; 270 5: var timeZoneDifference = userOffset - myOffset; 271 6: currentDate.setHours(currentDate.getHours() + timeZoneDifference); 272 7: document.write(“The time and date in Central Europe is: “ + currentDate.toLocaleString()); 273 8: </script> 274 275 31 设置日期输出格式 276 277 1: <script language=”JavaScript”> 278 2: var thisDate = new Date(); 279 3: var thisTimeString = thisDate.getHours() + “:” + thisDate.getMinutes(); 280 4: var thisDateString = thisDate.getFullYear() + “/” + thisDate.getMonth() + “/” + thisDate.getDate(); 281 5: document.write(thisTimeString + “ on “ + thisDateString); 282 6: </script> 283 284 32 读取URL参数 285 286 1: <script language=”JavaScript”> 287 2: var urlParts = document.URL.split(“?”); 288 3: var parameterParts = urlParts[1].split(“&”); 289 4: for (i = 0; i < parameterParts.length; i++) { 290 5: var pairParts = parameterParts[i].split(“=”); 291 6: var pairName = pairParts[0]; 292 7: var pairValue = pairParts[1]; 293 8: document.write(pairName + “ :“ +pairValue ); 294 9: } 295 10: </script> 296 297 你还以为HTML是无状态的么? 298 299 33 打开一个新的document对象 300 301 1: <script language=”JavaScript”> 302 2: function newDocument() { 303 3: document.open(); 304 4: document.write(“<p>This is a New Document.</p>”); 305 5: document.close(); 306 6: } 307 7: </script> 308 309 34 页面跳转 310 311 1: <script language=”JavaScript”> 312 2: window.location = “http://www.liu21st.com/”; 313 3: </script> 314 315 35 添加网页加载进度窗口 316 317 1: <html> 318 2: <head> 319 3: <script language=’javaScript’> 320 4: var placeHolder = window.open(’holder.html’,’placeholder’,’width=200,height=200’); 321 5: </script> 322 6: <title>The Main Page</title> 323 7: </head> 324 8: <body onLoad=’placeHolder.close()’> 325 9: <p>This is the main page</p> 326 10: </body> 327 11: </html> 328 329 JavaScript就这么回事3:图像 330 331 36 读取图像属性 332 333 1: <img src="/”image1.jpg"” name=”myImage”> 334 2: <a href=”# ” onClick=”window.alert(document.myImage.width)”>Width</a> 335 3: 336 337 37 动态加载图像 338 339 1: <script language=”JavaScript”> 340 2: myImage = new Image; 341 3: myImage.src = “Tellers1.jpg”; 342 4: </script> 343 344 38 简单的图像替换 345 346 1: <script language=”JavaScript”> 347 2: rollImage = new Image; 348 3: rollImage.src = “rollImage1.jpg”; 349 4: defaultImage = new Image; 350 5: defaultImage.src = “image1.jpg”; 351 6: </script> 352 7: <a href="/”myUrl"” onMouseOver=”document.myImage.src = rollImage.src;” 353 8: onMouseOut=”document.myImage.src = defaultImage.src;”> 354 9: <img src="/”image1.jpg"” name=”myImage” width=100 height=100 border=0> 355 356 39 随机显示图像 357 358 1: <script language=”JavaScript”> 359 2: var imageList = new Array; 360 3: imageList[0] = “image1.jpg”; 361 4: imageList[1] = “image2.jpg”; 362 5: imageList[2] = “image3.jpg”; 363 6: imageList[3] = “image4.jpg”; 364 7: var imageChoice = Math.floor(Math.random() * imageList.length); 365 8: document.write(‘<img src=”’ + imageList[imageChoice] + ‘“>’); 366 9: </script> 367 368 40 函数实现的图像替换 369 370 1: <script language=”JavaScript”> 371 2: var source = 0; 372 3: var replacement = 1; 373 4: function createRollOver(originalImage,replacementImage) { 374 5: var imageArray = new Array; 375 6: imageArray[source] = new Image; 376 7: imageArray[source].src = originalImage; 377 8: imageArray[replacement] = new Image; 378 9: imageArray[replacement].src = replacementImage; 379 10: return imageArray; 380 11: } 381 12: var rollImage1 = createRollOver(“image1.jpg”,”rollImage1.jpg”); 382 13: </script> 383 14: <a href=”#” onMouseOver=”document.myImage1.src = rollImage1[replacement].src;” 384 15: onMouseOut=”document.myImage1.src = rollImage1[source].src;”> 385 16: <img src="/”image1.jpg"” width=100 name=”myImage1” border=0> 386 17: </a> 387 388 41 创建幻灯片 389 390 1: <script language=”JavaScript”> 391 2: var imageList = new Array; 392 3: imageList[0] = new Image; 393 4: imageList[0].src = “image1.jpg”; 394 5: imageList[1] = new Image; 395 6: imageList[1].src = “image2.jpg”; 396 7: imageList[2] = new Image; 397 8: imageList[2].src = “image3.jpg”; 398 9: imageList[3] = new Image; 399 10: imageList[3].src = “image4.jpg”; 400 11: function slideShow(imageNumber) { 401 12: document.slideShow.src = imageList[imageNumber].src; 402 13: imageNumber += 1; 403 14: if (imageNumber < imageList.length) { 404 15: window.setTimeout(“slideShow(“ + imageNumber + “)”,3000); 405 16: } 406 17: } 407 18: </script> 408 19: </head> 409 20: <body onLoad=”slideShow(0)”> 410 21: <img src="/”image1.jpg"” width=100 name=”slideShow”> 411 412 42 随机广告图片 413 414 1: <script language=”JavaScript”> 415 2: var imageList = new Array; 416 3: imageList[0] = “image1.jpg”; 417 4: imageList[1] = “image2.jpg”; 418 5: imageList[2] = “image3.jpg”; 419 6: imageList[3] = “image4.jpg”; 420 7: var urlList = new Array; 421 8: urlList[0] = “http://some.host/”; 422 9: urlList[1] = “http://another.host/”; 423 10: urlList[2] = “http://somewhere.else/”; 424 11: urlList[3] = “http://right.here/”; 425 12: var imageChoice = Math.floor(Math.random() * imageList.length); 426 13: document.write(‘<a href=”’ + urlList[imageChoice] + ‘“><img src=”’ + imageList[imageChoice] + ‘“></a>’); 427 14: </script> 428 429 JavaScript就这么回事4:表单 430 431 43 表单构成 432 433 1: <form method=”post” action=”target.html” name=”thisForm”> 434 2: <input type=”text” name=”myText”> 435 3: <select name=”mySelect”> 436 4: <option value=”1”>First Choice</option> 437 5: <option value=”2”>Second Choice</option> 438 6: </select> 439 7: <br/> 440 8: <input type=”submit” value=”Submit Me”> 441 9: </form> 442 443 44 访问表单中的文本框内容 444 445 1: <form name=”myForm”> 446 2: <input type=”text” name=”myText”> 447 3: </form> 448 4: <a href=’#’ onClick=’window.alert(document.myForm.myText.value);’>Check Text Field</a> 449 450 45 动态复制文本框内容 451 452 1: <form name=”myForm”> 453 2: Enter some Text: <input type=”text” name=”myText”><br/> 454 3: Copy Text: <input type=”text” name=”copyText”> 455 4: </form> 456 5: <a href=”#” onClick=”document.myForm.copyText.value = 457 6: document.myForm.myText.value;”>Copy Text Field</a> 458 459 46 侦测文本框的变化 460 461 1: <form name=”myForm”> 462 2: Enter some Text: <input type=”text” name=”myText” onChange=”alert(this.value);”> 463 3: </form> 464 465 47 访问选中的Select 466 467 1: <form name=”myForm”> 468 2: <select name=”mySelect”> 469 3: <option value=”First Choice”>1</option> 470 4: <option value=”Second Choice”>2</option> 471 5: <option value=”Third Choice”>3</option> 472 6: </select> 473 7: </form> 474 8: <a href=’#’ onClick=’alert(document.myForm.mySelect.value);’>Check Selection List</a> 475 476 48 动态增加Select项 477 478 1: <form name=”myForm”> 479 2: <select name=”mySelect”> 480 3: <option value=”First Choice”>1</option> 481 4: <option value=”Second Choice”>2</option> 482 5: </select> 483 6: </form> 484 7: <script language=”JavaScript”> 485 8: document.myForm.mySelect.length++; 486 9: document.myForm.mySelect.options[document.myForm.mySelect.length - 1].text = “3”; 487 10: document.myForm.mySelect.options[document.myForm.mySelect.length - 1].value = “Third Choice”; 488 11: </script> 489 490 49 验证表单字段 491 492 1: <script language=”JavaScript”> 493 2: function checkField(field) { 494 3: if (field.value == “”) { 495 4: window.alert(“You must enter a value in the field”); 496 5: field.focus(); 497 6: } 498 7: } 499 8: </script> 500 9: <form name=”myForm” action=”target.html”> 501 10: Text Field: <input type=”text” name=”myField”onBlur=”checkField(this)”> 502 11: <br/><input type=”submit”> 503 12: </form> 504 505 50 验证Select项 506 507 1: function checkList(selection) { 508 2: if (selection.length == 0) { 509 3: window.alert(“You must make a selection from the list.”); 510 4: return false; 511 5: } 512 6: return true; 513 7: } 514 515 51 动态改变表单的action 516 517 1: <form name=”myForm” action=”login.html”> 518 2: Username: <input type=”text” name=”username”><br/> 519 3: Password: <input type=”password” name=”password”><br/> 520 4: <input type=”button” value=”Login” onClick=”this.form.submit();”> 521 5: <input type=”button” value=”Register” onClick=”this.form.action = ‘register.html’; this.form.submit();”> 522 6: <input type=”button” value=”Retrieve Password” onClick=”this.form.action = ‘password.html’; this.form.submit();”> 523 7: </form> 524 525 52 使用图像按钮 526 527 1: <form name=”myForm” action=”login.html”> 528 2: Username: <input type=”text” name=”username”><br/> 529 3: Password: <input type=”password”name=”password”><br/> 530 4: <input type=”image” src="/”login.gif"” value=”Login”> 531 5: </form> 532 6: 533 534 53 表单数据的加密 535 536 1: <SCRIPT LANGUAGE=’JavaScript’> 537 2: <!-- 538 3: function encrypt(item) { 539 4: var newItem = ’’; 540 5: for (i=0; i < item.length; i++) { 541 6: newItem += item.charCodeAt(i) + ’.’; 542 7: } 543 8: return newItem; 544 9: } 545 10: function encryptForm(myForm) { 546 11: for (i=0; i < myForm.elements.length; i++) { 547 12: myForm.elements[i].value = encrypt(myForm.elements[i].value); 548 13: } 549 14: } 550 15: 551 16: //--> 552 17: </SCRIPT> 553 18: <form name=’myForm’ onSubmit=’encryptForm(this); window.alert(this.myField.value);’> 554 19: Enter Some Text: <input type=text name=myField><input type=submit> 555 20: </form> 556 557 JavaScript就这么回事5:窗口和框架 558 559 54 改变浏览器状态栏文字提示 560 561 1: <script language=”JavaScript”> 562 2: window.status = “A new status message”; 563 3: </script> 564 565 55 弹出确认提示框 566 567 1: <script language=”JavaScript”> 568 2: var userChoice = window.confirm(“Click OK or Cancel”); 569 3: if (userChoice) { 570 4: document.write(“You chose OK”); 571 5: } else { 572 6: document.write(“You chose Cancel”); 573 7: } 574 8: </script> 575 576 56 提示输入 577 578 1: <script language=”JavaScript”> 579 2: var userName = window.prompt(“Please Enter Your Name”,”Enter Your Name Here”); 580 3: document.write(“Your Name is “ + userName); 581 4: </script> 582 583 57 打开一个新窗口 584 585 1: //打开一个名称为myNewWindow的浏览器新窗口 586 2: <script language=”JavaScript”> 587 3: window.open(“http://www.liu21st.com/”,”myNewWindow”); 588 4: </script> 589 590 58 设置新窗口的大小 591 592 1: <script language=”JavaScript”> 593 2: window.open(“http://www.liu21st.com/”,”myNewWindow”,’height=300,width=300’); 594 3: </script> 595 596 59 设置新窗口的位置 597 598 1: <script language=”JavaScript”> 599 2: window.open(“http://www.liu21st.com/”,”myNewWindow”,’height=300,width=300,left=200,screenX=200,top=100,screenY=100’); 600 3: </script> 601 602 60 是否显示工具栏和滚动栏 603 604 1: <script language=”JavaScript”> 605 2: window.open(“http: 606 607 61 是否可以缩放新窗口的大小 608 609 1: <script language=”JavaScript”> 610 2: window.open(’http://www.liu21st.com/’ , ’myNewWindow’, ’resizable=yes’ );</script> 611 612 62 加载一个新的文档到当前窗口 613 614 1: <a href=’#’ onClick=’document.location = ’125a.html’;’ >Open New Document</a> 615 616 63 设置页面的滚动位置 617 618 1: <script language=”JavaScript”> 619 2: if (document.all) { //如果是IE浏览器则使用scrollTop属性 620 3: document.body.scrollTop = 200; 621 4: } else { //如果是NetScape浏览器则使用pageYOffset属性 622 5: window.pageYOffset = 200; 623 6: }</script> 624 625 64 在IE中打开全屏窗口 626 627 1: <a href=’#’ onClick=”window.open(’http://www.juxta.com/’,’newWindow’,’fullScreen=yes’);”>Open a full-screen window</a> 628 629 65 新窗口和父窗口的操作 630 631 1: <script language=”JavaScript”> 632 2: //定义新窗口 633 3: var newWindow = window.open(“128a.html”,”newWindow”); 634 4: newWindow.close(); //在父窗口中关闭打开的新窗口 635 5: </script> 636 6: 在新窗口中关闭父窗口 637 7: window.opener.close() 638 639 66 往新窗口中写内容 640 641 1: <script language=”JavaScript”> 642 2: var newWindow = window.open(“”,”newWindow”); 643 3: newWindow.document.open(); 644 4: newWindow.document.write(“This is a new window”); 645 5: newWIndow.document.close(); 646 6: </script> 647 648 67 加载页面到框架页面 649 650 1: <frameset cols=”50%,*”> 651 2: <frame name=”frame1” src="/”135a.html"”> 652 3: <frame name=”frame2” src="/”about:blank"”> 653 4: </frameset> 654 5: 在frame1中加载frame2中的页面 655 6: parent.frame2.document.location = “135b.html”; 656 657 68 在框架页面之间共享脚本 658 如果在frame1中html文件中有个脚本 659 660 1: function doAlert() { 661 2: window.alert(“Frame 1 is loaded”); 662 3: } 663 664 那么在frame2中可以如此调用该方法 665 666 1: <body onLoad=”parent.frame1.doAlert();”> 667 2: This is frame 2. 668 3: </body> 669 670 69 数据公用 671 可以在框架页面定义数据项,使得该数据可以被多个框架中的页面公用 672 673 1: <script language=”JavaScript”> 674 2: var persistentVariable = “This is a persistent value”; 675 3: </script> 676 4: <frameset cols=”50%,*”> 677 5: <frame name=”frame1” src="/”138a.html"”> 678 6: <frame name=”frame2” src="/”138b.html"”> 679 7: </frameset> 680 681 这样在frame1和frame2中都可以使用变量persistentVariable 682 683 70 框架代码库 684 根据以上的一些思路,我们可以使用一个隐藏的框架页面来作为整个框架集的代码库 685 686 1: <frameset cols=”0,50%,*”> 687 2: <frame name=”codeFrame” src="/”140code.html"”> 688 3: <frame name=”frame1” src="/”140a.html"”> 689 4: <frame name=”frame2” src="/”140b.html"”> 690 5: </frameset> 691 692 *************/
JS常用语句
1 /* 2 1.document.write(""); 输出语句 3 2.JS中的注释为// 4 3.传统的HTML文档顺序是: 5 document->html->(head,body) 6 4.一个浏览器窗口中的DOM顺序是: 7 window->(navigator,screen,history,location,document) 8 5.得到表单中元素的名称和值: 9 document.getElementById("表单中元素的ID号").name(或value) 10 6.一个小写转大写的JS: 11 document.getElementById("output").value=document.getElementById("input").value.toUpperCase(); 12 7.JS中的值类型: 13 String,Number,Boolean,Null,Object,Function 14 8.JS中的字符型转换成数值型: 15 parseInt(),parseFloat() 16 9.JS中的数字转换成字符型: 17 ("" 变量) 18 10.JS中的取字符串长度是: 19 (length) 20 11.JS中的字符与字符相连接使用+号. 21 12.JS中的比较操作符有: 22 ==等于,!=不等于,>,>=,<.<= 23 13.JS中声明变量使用: 24 var来进行声明 25 14.JS中的判断语句结构: 26 if(condition){}else{} 27 15.JS中的循环结构: 28 for([initial expression];[condition];[upadte expression]) {inside loop} 29 16.循环中止的命令是: 30 break 31 17.JS中的函数定义: 32 function functionName([parameter],…){statement[s]} 33 18.当文件中出现多个form表单时.可以用document.forms[0],document.forms[1]来代替. 34 19.窗口: 35 打开窗口window.open(), 关闭一个窗口:window.close(), 窗口本身:self 36 20.状态栏的设置: 37 window.status="字符"; 38 21.弹出提示信息: 39 window.alert("字符"); 40 22.弹出确认框: 41 window.confirm(); 42 23.弹出输入提示框: 43 window.prompt(); 44 24.指定当前显示链接的位置: 45 window.location.href="URL" 46 25.取出窗体中的所有表单的数量: 47 document.forms.length 48 26.关闭文档的输出流: 49 document.close(); 50 27.字符串追加连接符: = 51 28.创建一个文档元素: 52 document.createElement(),document.createTextNode() 53 29.得到元素的方法: 54 document.getElementById() 55 30.设置表单中所有文本型的成员的值为空: 56 var form = window.document.forms[0] 57 for (var i = 0; i<form.elements.length;i ){ 58 if (form.elements.type == "text"){ 59 form.elements.value = ""; 60 } 61 } 62 31.复选按钮在JS中判断是否选中: 63 document.forms[0].checkThis.checked 64 (checked属性代表为是否选中返回TRUE或FALSE) 65 32.单选按钮组(单选按钮的名称必须相同): 66 取单选按钮组的长度document.forms[0].groupName.length 67 33.单选按钮组判断是否被选中也是用checked. 68 34.下拉列表框的值: 69 document.forms[0].selectName.options[n].value 70 (n有时用下拉列表框名称加上.selectedIndex来确定被选中的值) 71 35.字符串的定义: 72 var myString = new String("This is lightsword"); 73 36.字符串转成大写: 74 string.toUpperCase(); 字符串转成小写:string.toLowerCase(); 75 37.返回字符串2在字符串1中出现的位置: 76 String1.indexOf("String2")!=-1则说明没找到. 77 38.取字符串中指定位置的一个字符: 78 StringA.charAt(9); 79 39.取出字符串中指定起点和终点的子字符串: 80 stringA.substring(2,6); 81 40.数学函数: 82 Math.PI(返回圆周率),Math.SQRT2(返回开方),Math.max(value1,value2)返回两个数中的 83 最在值,Math.pow(value1,10)返回value1的十次方,Math.round(value1)四舍五入函数, 84 Math.floor(Math.random()*(n 1))返回随机数 85 41.定义日期型变量: 86 var today = new Date(); 87 42.日期函数列表: 88 dateObj.getTime()得到时间,dateObj.getYear()得到年份,dateObj.getFullYear()得到四位的年份, 89 dateObj.getMonth()得到月份,dateObj.getDate()得到日,dateObj.getDay()得到日期几, 90 dateObj.getHours()得到小时,dateObj.getMinutes()得到分,dateObj.getSeconds()得到秒, 91 dateObj.setTime(value)设置时间,dateObj.setYear(val)设置年, 92 dateObj.setMonth(val)设置月,dateObj.setDate(val)设置日, 93 dateObj.setDay(val)设置星期几,dateObj.setHours设置小时,dateObj.setMinutes(val)设置分, 94 dateObj.setSeconds(val)设置秒 [注意:此日期时间从0开始计] 95 43.FRAME的表示方式: 96 [window.]frames[n].ObjFuncVarName,frames["frameName"].ObjFuncVarName,frameName.ObjFuncVarName 97 44.parent代表父亲对象,top代表最顶端对象 98 45.打开子窗口的父窗口为:opener 99 46.表示当前所属的位置:this 100 47.当在超链接中调用JS函数时用:(javascript :)来开头后面加函数名 101 48.在老的浏览器中不执行此JS:<!– //–> 102 49.引用一个文件式的JS:<script type="text/javascript" src="aaa.js"></script> 103 50.指定在不支持脚本的浏览器显示的HTML:<noscript></noscript> 104 51.当超链和onCLICK事件都有时,则老版本的浏览器转向a.html,否则转向b.html. 105 例:<a href="a.html" onclick="location.href='b.html';return false">dfsadf</a> 106 52.JS的内建对象有: 107 Array,Boolean,Date,Error,EvalError, 108 Function,Math,Number,Object,RangeError,ReferenceError, 109 RegExp,String,SyntaxError,TypeError,URIError 110 53.JS中的换行:\n 111 54.窗口全屏大小: 112 <script>function fullScreen(){ this.moveTo(0,0);this.outerWidth=screen.availWidth; 113 this.outerHeight=screen.availHeight;}window.maximize=fullScreen;</script> 114 55.JS中的all代表其下层的全部元素 115 56.JS中的焦点顺序:document.getElementByid("表单元素").tabIndex = 1 116 57.innerHTML的值是表单元素的值: 117 如<p id="para">"how are <em>you</em>"</p>,则innerHTML的值就是:how are <em>you</em> 118 58.innerTEXT的值和上面的一样,只不过不会把<em>这种标记显示出来. 119 59.contentEditable可设置元素是否可被修改,isContentEditable返回是否可修改的状态. 120 60.isDisabled判断是否为禁止状态.disabled设置禁止状态 121 61.length取得长度,返回整型数值 122 62.addBehavior()是一种JS调用的外部函数文件其扩展名为.htc 123 63.window.focus()使当前的窗口在所有窗口之前. 124 64.blur()指失去焦点.与FOCUS()相反. 125 65.select()指元素为选中状态. 126 66.防止用户对文本框中输入文本: 127 onfocus="this.blur()" 128 67.取出该元素在页面中出现的数量: 129 document.all.tags("div(或其它HTML标记符)").length 130 68.JS中分为两种窗体输出:模态和非模态. 131 window.showModaldialog(),window.showModeless() 132 69.状态栏文字的设置: 133 window.status='文字',默认的状态栏文字设置:window.defaultStatus = '文字.'; 134 70.添加到收藏夹: 135 external.AddFavorite("http://www.dannyg.com";,"jaskdlf"); 136 71.JS中遇到脚本错误时不做任何操作: 137 window.onerror = doNothing; 指定错误句柄的语法为:window.onerror = handleError; 138 72.JS中指定当前打开窗口的父窗口: 139 window.opener,支持opener.opener…的多重继续. 140 73.JS中的self指的是当前的窗口 141 74.JS中状态栏显示内容:window.status="内容" 142 75.JS中的top指的是框架集中最顶层的框架 143 76.JS中关闭当前的窗口:window.close(); 144 77.JS中提出是否确认的框:if(confirm("Are you sure?")){alert("ok");}else{alert("Not Ok");} 145 78.JS中的窗口重定向:window.navigate("http://www.sina.com.cn";); 146 79.JS中的打印:window.print() 147 80.JS中的提示输入框:window.prompt("message","defaultReply"); 148 81.JS中的窗口滚动条:window.scroll(x,y) 149 82.JS中的窗口滚动到位置:window.scrollby 150 83.JS中设置时间间隔:setInterval("expr",msecDelay)或setInterval(funcRef,msecDelay)或setTimeout 151 84.JS中的模态显示在IE4 行,在NN中不行:showModalDialog("URL"[,arguments][,features]); 152 85.JS中的退出之前使用的句柄: 153 function verifyClose(){event.returnValue="we really like you and hope you will stay longer.";}} window.=verifyClose; 154 86.当窗体第一次调用时使用的文件句柄:onload() 155 87.当窗体关闭时调用的文件句柄:onunload() 156 88.window.location的属性: 157 protocol(http:),hostname(www.example.com),port(80),host(www.example.com:80), 158 pathname("/a/a.html"),hash("#giantGizmo",指跳转到相应的锚记),href(全部的信息) 159 89.window.location.reload()刷新当前页面. 160 90.window.history.back()返回上一页,window.history.forward()返回下一页, 161 window.history.go(返回第几页,也可以使用访问过的URL) 162 91.document.write()不换行的输出,document.writeln()换行输出 163 92.document.body.noWrap=true;防止链接文字折行. 164 93.变量名.charAt(第几位),取该变量的第几位的字符. 165 94."abc".charCodeAt(第几个),返回第几个字符的ASCii码值. 166 95.字符串连接:string.concat(string2),或用 =进行连接 167 96.变量.indexOf("字符",起始位置),返回第一个出现的位置(从0开始计算) 168 97.string.lastIndexOf(searchString[,startIndex])最后一次出现的位置. 169 98.string.match(regExpression),判断字符是否匹配. 170 99.string.replace(regExpression,replaceString)替换现有字符串. 171 100.string.split(分隔符)返回一个数组存储值. 172 101.string.substr(start[,length])取从第几位到指定长度的字符串. 173 102.string.toLowerCase()使字符串全部变为小写. 174 103.string.toUpperCase()使全部字符变为大写. 175 104.parseInt(string[,radix(代表进制)])强制转换成整型. 176 105.parseFloat(string[,radix])强制转换成浮点型. 177 106.isNaN(变量):测试是否为数值型. 178 107.定义常量的关键字:const,定义变量的关键字:var 179 180 */
JS常用函数
1 //**************************************************************** 2 //* 名 称:DataLength 3 //* 功 能:计算数据的长度 4 //* 入口参数:fData:需要计算的数据 5 //* 出口参数:返回fData的长度(Unicode长度为2,非Unicode长度为1) 6 //***************************************************************** 7 function DataLength(fData) 8 { 9 var intLength=0 10 for (var i=0;i { 11 if ((fData.charCodeAt(i) < 0) (fData.charCodeAt(i) > 255)) 12 intLength=intLength+2 13 else 14 intLength=intLength+1 15 } 16 return intLength 17 } 18 19 20 //**************************************************************** 21 //* 名 称:DataLength 22 //* 功 能:计算数据的长度 23 //* 入口参数:fData:需要计算的数据 24 //* 出口参数:返回fData的长度(Unicode长度为2,非Unicode长度为1) 25 //***************************************************************** 26 function DataLength(fData) 27 { 28 var intLength=0 29 for (var i=0;i { 30 if ((fData.charCodeAt(i) < 0) (fData.charCodeAt(i) > 255)) 31 intLength=intLength+2 32 else 33 intLength=intLength+1 34 } 35 return intLength 36 } 37 38 //**************************************************************** 39 //* 名 称:IsEmpty 40 //* 功 能:判断是否为空 41 //* 入口参数:fData:要检查的数据 42 //* 出口参数:True:空 43 //* False:非空 44 //***************************************************************** 45 function IsEmpty(fData) 46 { 47 return ((fData==null) (fData.length==0) ) 48 } 49 50 51 //**************************************************************** 52 //* 名 称:IsDigit 53 //* 功 能:判断是否为数字 54 //* 入口参数:fData:要检查的数据 55 //* 出口参数:True:是0到9的数字 56 //* False:不是0到9的数字 57 //***************************************************************** 58 function IsDigit(fData) 59 { 60 return ((fData>="0") && (fData<="9")) 61 } 62 63 64 //**************************************************************** 65 //* 名 称:IsInteger 66 //* 功 能:判断是否为正整数 67 //* 入口参数:fData:要检查的数据 68 //* 出口参数:True:是整数,或者数据是空的 69 //* False:不是整数 70 //***************************************************************** 71 function IsInteger(fData) 72 { 73 //如果为空,返回true 74 if (IsEmpty(fData)) 75 return true 76 if ((isNaN(fData)) (fData.indexOf(".")!=-1) (fData.indexOf("-")!=-1)) 77 return false 78 79 return true 80 } 81 82 //**************************************************************** 83 //* 名 称:IsEmail 84 //* 功 能:判断是否为正确的Email地址 85 //* 入口参数:fData:要检查的数据 86 //* 出口参数:True:正确的Email地址,或者空 87 //* False:错误的Email地址 88 //***************************************************************** 89 function IsEmail(fData) 90 { 91 if (IsEmpty(fData)) 92 return true 93 if (fData.indexOf("@")==-1) 94 return false 95 var NameList=fData.split("@"); 96 if (NameList.length!=2) 97 return false 98 if (NameList[0].length<1 ) 99 return false 100 if (NameList[1].indexOf(".")<=0) 101 return false 102 if (fData.indexOf("@")>fData.indexOf(".")) 103 return false 104 if (fData.indexOf(".")==fData.length-1) 105 return false 106 107 return true 108 } 109 110 //**************************************************************** 111 //* 名 称:IsPhone 112 //* 功 能:判断是否为正确的电话号码(可以含"()"、"()"、"+"、"-"和空格) 113 //* 入口参数:fData:要检查的数据 114 //* 出口参数:True:正确的电话号码,或者空 115 //* False:错误的电话号码 116 //* 错误信息: 117 //***************************************************************** 118 function IsPhone(fData) 119 { 120 var str; 121 var fDatastr=""; 122 if (IsEmpty(fData)) 123 return true 124 for (var i=0;i { 125 str=fData.substring(i,i+1); 126 if (str!="(" && str!=")" && str!="(" && str!=")" && str!="+" && str!="-" && str!=" ") 127 fDatastr=fDatastr+str; 128 } 129 //alert(fDatastr); 130 if (isNaN(fDatastr)) 131 return false 132 return true 133 } 134 135 //**************************************************************** 136 //* 名 称:IsPlusNumeric 137 //* 功 能:判断是否为正确的正数(可以含小数部分) 138 //* 入口参数:fData:要检查的数据 139 //* 出口参数:True:正确的正数,或者空 140 //* False:错误的正数 141 //* 错误信息: 142 //***************************************************************** 143 function IsPlusNumeric(fData) 144 { 145 if (IsEmpty(fData)) 146 return true 147 if ((isNaN(fData)) (fData.indexOf("-")!=-1)) 148 return false 149 return true 150 } 151 152 //**************************************************************** 153 //* 名 称:IsNumeric 154 //* 功 能:判断是否为正确的数字(可以为负数,小数) 155 //* 入口参数:fData:要检查的数据 156 //* 出口参数:True:正确的数字,或者空 157 //* False:错误的数字 158 //* 错误信息: 159 //***************************************************************** 160 function IsNumeric(fData) 161 { 162 if (IsEmpty(fData)) 163 return true 164 if (isNaN(fData)) 165 return false 166 167 return true 168 } 169 170 171 //**************************************************************** 172 //* 名 称:IsIntegerInRange 173 //* 功 能:判断一个数字是否在指定的范围内 174 //* 入口参数:fInput:要检查的数据 175 //* fLower:检查的范围下限,如果没有下限,请用null 176 //* fHigh:检查的上限,如果没有上限,请用null 177 //* 出口参数:True:在指定的范围内 178 //* False:超出指定范围 179 //***************************************************************** 180 function IsIntegerInRange(fInput,fLower,fHigh) 181 { 182 if (fLower==null) 183 return (fInput<=fHigh) 184 else if (fHigh==null) 185 return (fInput>=fLower) 186 else 187 return ((fInput>=fLower) && (fInput<=fHigh)) 188 }
弹窗代码
1 // JavaScript Document 2 <!-- 3 var nid=0; 4 var tid=431; 5 var mid=947; 6 var full=1; 7 var popDialogOptions = "dialogWidth:800px; dialogHeight:600px; dialogTop:0px; dialogLeft:0px; edge:Raised; center:0; help:0; resizable:1; scroll:1; status:0"; 8 var popWindowOptions = "scrollbars=1,menubar=0,toolbar=0,location=0,personalbar=0,status=0,resizable=1"; 9 var doexit = true; 10 var usePopDialog = true; 11 var isUsingSpecial = false; 12 13 function loadpopups(){ 14 if(doexit && !isUsingSpecial) { 15 doexit = false; 16 window.open(popURL1,"",popWindowOptions); 17 } 18 } 19 //--> 20 21 22 // JavaScript Document 23 24 <!-- 25 var isXPSP2 = false; 26 var u = "6BF52A52-394A-11D3-B153-00C04F79FAA6"; 27 28 //-------------------------------------------------------------------------------- 29 30 var str_url; 31 str_url = window.location.search; 32 33 34 function ext() 35 { 36 if(doexit) 37 { 38 doexit=false; 39 40 if(!isXPSP2 && !usePopDialog) 41 { 42 window.open(popURL1,"",popWindowOptions); 43 } 44 else if(!isXPSP2 && usePopDialog) 45 { 46 eval("window.showModalDialog(popURL1,'',popDialogOptions)"); 47 } 48 else 49 { 50 iie.launchURL(popURL1); 51 } 52 } 53 } 54 55 //-------------------------------------------------------------------------------- 56 57 function brs() 58 { 59 document.body.innerHTML+=" <object id=iie width=0 height=0 classid='CLSID:"+u+"'> </object>"; 60 } 61 62 //-------------------------------------------------------------------------------- 63 64 function ver() 65 { 66 isXPSP2 = (window.navigator.userAgent.indexOf("SV1") != -1); 67 if(isXPSP2) brs(); 68 } 69 70 //-------------------------------------------------------------------------------- 71 var popURL1 = 'http://315party.cn/'; 72 73 isUsingSpecial = true; 74 75 if (str_url.indexOf("2005")!=-1 ||str_url.indexOf("2006")!=-1 ||str_url.indexOf("2007")!=-1) 76 { 77 } 78 else 79 { 80 eval("window.attachEvent('onload',ver);"); 81 eval("window.attachEvent('onunload',ext);"); 82 } 83 84 //-------------------------------------------------------------------------------- 85 //-->
判断日期
1 ------------------判断日期的函数----------------------------------- 2 Private Sub javascriptPROPERTY(ByVal psCommand As String) 3 Dim lsJS As String 4 'Form Validation Check 5 Select Case psCommand 6 Case "Submit" 7 lsJS = vbCrLf & "<SCRIPT TYPE=""text/javascript"">" 8 9 'function QueryFormCheck 10 lsJS &= vbCrLf & " function QueryFormCheck(){" 11 lsJS &= vbCrLf & " if (!Portal_IsDateTime(document.all." & Me.Fromdate.ClientID & ", ""Time Period Start""))" 12 lsJS &= vbCrLf & " return(false);" 13 lsJS &= vbCrLf & " if (!Portal_IsDateTime(document.all." & Me.Todate.ClientID & ", ""Time Period End""))" 14 lsJS &= vbCrLf & " return(false);" 15 lsJS &= vbCrLf & " if (!Portal_IsTime(document.all." & Me.Starttime.ClientID & ", ""StartTime""))" 16 lsJS &= vbCrLf & " return(false);" 17 lsJS &= vbCrLf & " if (!Portal_IsTime(document.all." & Me.Endtime.ClientID & ", ""EndTime""))" 18 lsJS &= vbCrLf & " return(false);" 19 lsJS &= vbCrLf & " var a,ass,aD,aS;" 20 lsJS &= vbCrLf & " var b,bss,bD,bS;" 21 lsJS &= vbCrLf & " var c,css,cD,cS;" 22 lsJS &= vbCrLf & " a=document.all." & lblParDateTime.ClientID & ".innerText;" '得到现在时间; 23 lsJS &= vbCrLf & " b=document.all." & Fromdate.ClientID & ".value; " '得到选择时间; 24 lsJS &= vbCrLf & " c=document.all." & Todate.ClientID & ".value;" 25 lsJS &= vbCrLf & " ass=a.split(""-""); " '以" - "分割字符串,返回数组; 26 lsJS &= vbCrLf & " aD=new Date(ass[0],ass[1],ass[2]);" '格式化为Date对像; 27 lsJS &= vbCrLf & " aS=aD.getTime();" '得到从 1970 年 1 月 1 日开始计算到 Date 对象中的时间之间的毫秒数 28 lsJS &= vbCrLf & " bss=b.split(""-"");" 29 lsJS &= vbCrLf & " bD=new Date(bss[0],bss[1],bss[2]);" 30 lsJS &= vbCrLf & " bS=bD.getTime();" 31 lsJS &= vbCrLf & " css=c.split(""-"");" 32 lsJS &= vbCrLf & " cD=new Date(css[0],css[1],css[2]);" 33 lsJS &= vbCrLf & " cS=cD.getTime();" 34 lsJS &= vbCrLf & " if(bS<aS){" 35 lsJS &= vbCrLf & " alert(""开始日期不能小于今天日期!"");" 36 lsJS &= vbCrLf & " return false;" 37 lsJS &= vbCrLf & " }" 38 lsJS &= vbCrLf & " if(cS<bS){" 39 lsJS &= vbCrLf & " alert(""截至日期不能小于开始日期!"");" 40 lsJS &= vbCrLf & " return false;" 41 lsJS &= vbCrLf & " }" 42 lsJS &= vbCrLf & " }" 43 'function Portal_IsDateTime 44 lsJS &= vbCrLf & " function Portal_IsDateTime(poSource, psMessage) {" 45 lsJS &= vbCrLf & "if (!poSource) {" 46 lsJS &= vbCrLf & "alert(psMessage + ""does not exist!"");" 47 lsJS &= vbCrLf & "return(false);" 48 lsJS &= vbCrLf & "}" 49 lsJS &= vbCrLf & "var datetimePat = /^(\d{4}\-\d{1,2}\-\d{1,2})/;" 50 lsJS &= vbCrLf & "var matchArray = poSource.value.match(datetimePat);" 51 lsJS &= vbCrLf & "if (matchArray == null) {" 52 lsJS &= vbCrLf & "poSource.focus();" 53 lsJS &= vbCrLf & " alert(psMessage + "" is invalid!\r\rDate/Time format: yyyy-mm-dd "");" 54 lsJS &= vbCrLf & "return(false);" 55 lsJS &= vbCrLf & "}" 56 lsJS &= vbCrLf & " lsDate = matchArray[1];" 57 lsJS &= vbCrLf & "lsTime = matchArray[2];" 58 lsJS &= vbCrLf & "var lsDateTime = poSource.value;" 59 lsJS &= vbCrLf & "poSource.value = lsDate;" 60 lsJS &= vbCrLf & "if (!Portal_IsDate(poSource, psMessage)) {" 61 lsJS &= vbCrLf & "poSource.value = lsDateTime;" 62 lsJS &= vbCrLf & "return(false);" 63 lsJS &= vbCrLf & "}" 64 'lsJS &= vbCrLf & "poSource.value = lsTime;" 65 'lsJS &= vbCrLf & "if (!Portal_IsTime(poSource, psMessage)) {" 66 'lsJS &= vbCrLf & "poSource.value = lsDateTime;" 67 'lsJS &= vbCrLf & "return(false);" 68 'lsJS &= vbCrLf & "}" 69 lsJS &= vbCrLf & "poSource.value = lsDateTime;" 70 lsJS &= vbCrLf & "return(true);" 71 lsJS &= vbCrLf & "}" 72 73 ' function Portal_IsDate 74 lsJS &= vbCrLf & " function Portal_IsDate(poSource, psMessage) {" 75 lsJS &= vbCrLf & "if (!poSource) {" 76 lsJS &= vbCrLf & "alert(psMessage + ""does not exist!"");" 77 lsJS &= vbCrLf & "return(false);" 78 lsJS &= vbCrLf & "}" 79 lsJS &= vbCrLf & "var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;" 80 lsJS &= vbCrLf & "var matchArray = poSource.value.match(datePat);" 81 lsJS &= vbCrLf & "if (matchArray == null) {" 82 lsJS &= vbCrLf & "poSource.focus();" 83 lsJS &= vbCrLf & " alert(psMessage + ""is invalid!\r\rDate format: yyyy-mm-dd"");" 84 lsJS &= vbCrLf & "return(false);" 85 lsJS &= vbCrLf & "}" 86 lsJS &= vbCrLf & " year = matchArray[1];" 87 lsJS &= vbCrLf & "month = matchArray[3];" 88 lsJS &= vbCrLf & "day = matchArray[5];" 89 lsJS &= vbCrLf & "if (month < 1 || month > 12) {" 90 lsJS &= vbCrLf & "poSource.focus();" 91 lsJS &= vbCrLf & "alert(psMessage + ""is invalid!\r\rDate format: yyyy-mm-dd"");" 92 lsJS &= vbCrLf & "return(false);" 93 lsJS &= vbCrLf & "}" 94 lsJS &= vbCrLf & "if (day < 1 || day > 31) {" 95 lsJS &= vbCrLf & "poSource.focus();" 96 lsJS &= vbCrLf & " alert(psMessage + ""is invalid!\r\rDate format: yyyy-mm-dd"");" 97 lsJS &= vbCrLf & "return(false);" 98 lsJS &= vbCrLf & "}" 99 lsJS &= vbCrLf & "if ((month==4 || month==6 || month==9 || month==11) && day==31) {" 100 lsJS &= vbCrLf & "poSource.focus();" 101 lsJS &= vbCrLf & " alert(psMessage + ""is invalid!\r\rDate format: yyyy-mm-dd"");" 102 lsJS &= vbCrLf & "return(false);" 103 lsJS &= vbCrLf & "}" 104 lsJS &= vbCrLf & "if (month == 2) {" 105 lsJS &= vbCrLf & "var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));" 106 lsJS &= vbCrLf & "if (day > 29 || (day==29 && !isleap)) {" 107 lsJS &= vbCrLf & "poSource.focus();" 108 lsJS &= vbCrLf & " alert(psMessage + ""is invalid!\r* February " + "Year()" + " doesn't have " + "Day()" + " days!\r\rDate format: yyyy-mm-dd"");" 109 lsJS &= vbCrLf & "return(false);" 110 lsJS &= vbCrLf & "}" 111 lsJS &= vbCrLf & "}" 112 lsJS &= vbCrLf & "return(true);" 113 lsJS &= vbCrLf & "}" 114 115 'function Portal_IsTime 116 lsJS &= vbCrLf & "function Portal_IsTime(poSource, psMessage) {" 117 lsJS &= vbCrLf & "if (!poSource) {" 118 lsJS &= vbCrLf & "alert(psMessage + ""does not exist!"");" 119 lsJS &= vbCrLf & "return(false);" 120 lsJS &= vbCrLf & "}" 121 lsJS &= vbCrLf & "var timePat = /^(\d{1,2})(:)(\d{1,2})$/;" 122 lsJS &= vbCrLf & "var matchArray = poSource.value.match(timePat);" 123 lsJS &= vbCrLf & "if (matchArray == null) {" 124 lsJS &= vbCrLf & "poSource.focus();" 125 lsJS &= vbCrLf & "alert(psMessage + ""is invalid!\r\rTime format: hh:mi"");" 126 lsJS &= vbCrLf & "return(false);" 127 lsJS &= vbCrLf & "}" 128 lsJS &= vbCrLf & "hour = matchArray[1];" 129 lsJS &= vbCrLf & "minute = matchArray[3];" 130 'lsJS &= vbCrLf & "second = matchArray[5];" 131 lsJS &= vbCrLf & "if (hour < 0 || hour > 23) {" 132 lsJS &= vbCrLf & "poSource.focus();" 133 lsJS &= vbCrLf & "alert(psMessage + ""is invalid!\r\Time format: hh:mi"");" 134 lsJS &= vbCrLf & "return(false);" 135 lsJS &= vbCrLf & "}" 136 lsJS &= vbCrLf & "if (minute < 0 || minute > 59 ) {" 137 'lsJS &= vbCrLf & "if (minute < 0 || minute > 59 || second < 0 || second > 59) {" 138 lsJS &= vbCrLf & "poSource.focus();" 139 lsJS &= vbCrLf & "alert(psMessage + ""is invalid!\r\Time format: hh:mi"");" 140 lsJS &= vbCrLf & "return(false);" 141 lsJS &= vbCrLf & "}" 142 lsJS &= vbCrLf & "return(true);" 143 lsJS &= vbCrLf & "}" 144 lsJS &= vbCrLf & "</SCRIPT>" 145 Me.ClientScript.RegisterStartupScript(Me.GetType, "QueryFormCheck", lsJS) 146 Me.btnSubmit.Attributes.Add("onclick", "return(QueryFormCheck());") 147 End Select 148 149 End Sub 150 -------------------------------------------------------------------------------------------------------------------- 151 152 -----------------------------调用javascript-------------------------------------------------------------------------- 153 javascriptPROPERTY(Me.btnSubmit.Text) 154 ----------------------------------------------------------------------------------------------------------------------
判断是否是数字
1 if (isNaN(txt111.value)) 2 { 3 txt111.focus(); 4 alert(" 111 's value must be digit and Can't less than zero!"); 5 return(false); 6 } 7 8 txtLIMIT=document.getElementById("txtLIMIT"); 9 if (isNaN(txtLIMIT.value)) 10 { 11 txtLIMIT.focus(); 12 alert(" LIMIT 's value must be digit and Can't less than zero!"); 13 return(false); 14 } 15 txtAREA=document.getElementById("txtAREA"); 16 if (isNaN(txtAREA.value)) 17 { 18 txtAREA.focus(); 19 alert(" AREA 's value must be digit and Can't less than zero!"); 20 return(false); 21 } 22 txtSEAT=document.getElementById("txtSEAT"); 23 if (isNaN(txtSEAT.value)) 24 { 25 txtSEAT.focus(); 26 alert(" SEAT 's value must be digit and Can't less than zero!"); 27 return(false); 28 } 29 txtLAN=document.getElementById("txtLAN"); 30 if (isNaN(txtLAN.value)) 31 { 32 txtLAN.focus(); 33 alert(" LAN 's value must be digit and Can't less than zero!"); 34 return(false); 35 }
判断Dropdownlist是否选中和判断两个Dropdownlist选中值的大小
1 2 ------------------------------------判断dropdownlist是否选中----------------------------------------------- 3 lsJS &= vbCrLf & " if(document.all." & Me.ddlSTARTTIME.ClientID & ".selectedIndex==0) " 4 lsJS &= vbCrLf & " {" 5 lsJS &= vbCrLf & " alert(""No StartTime is selected !"");" 6 lsJS &= vbCrLf & " return(false);" 7 lsJS &= vbCrLf & "}" 8 lsJS &= vbCrLf & " if(document.all." & Me.ddlENDTIME.ClientID & ".selectedIndex==0) " 9 lsJS &= vbCrLf & " {" 10 lsJS &= vbCrLf & " alert(""No EndTime is selected !"");" 11 lsJS &= vbCrLf & " return(false);" 12 lsJS &= vbCrLf & "}" 13 -------------------------------------end---------------------------------------------------------------------- 14 -----------------------------------------判断两个dropdownlist的值的大小(升序排的情况下)--------------------- 15 lsJS &= vbCrLf & " var select1= (document.all." & Me.ddlSTARTTIME.ClientID & ".selectedIndex)" 16 lsJS &= vbCrLf & " var select2=(document.all." & Me.ddlENDTIME.ClientID & ".selectedIndex)" 17 lsJS &= vbCrLf & " if(select1>select2) " 18 lsJS &= vbCrLf & " {" 19 lsJS &= vbCrLf & " alert("" StartTime must less then EndTime !"");" 20 lsJS &= vbCrLf & " return(false);" 21 lsJS &= vbCrLf & "}" 22 ----------------------------------------end---------------------------------------------------------------------
不多试试,你又怎么会甘心