事半功倍系列之javascript

   1
   2第一章javascript简介
   3
   41.在地址栏输入javascript语句
   5
   6Javascript:Document.write("显示文字")
   7
   82.将javascript嵌入 HTML文档
   9
  10<script language=javascript>
  11document.bgColor="blue"
  12</script>
  13
  14第二章 使用变量和数组
  15
  161.声明变量
  17
  18<script language=javascripe>
  19Var answer1,answer2,answer3,answer4;
  20answer1=9;
  21answer2=2.5
  22answer3="Milkey May" 
  23answer4=true
  24</script>
  25
  262.使用整数
  27
  28<script language=javascript>
  29var decimalNum,hexadecimalNum,octalNum
  30decimalNum=24
  31hexadecimalNum=0x24
  32octalNum=024
  33document.write("显示十进制数:"+ decimalNum+"<br>")
  34document.write("显示十六进制数:"+ hexadecimalNum +"<br>")
  35document.write("显示八进制数:"+ octalNum +"<br>")
  36</script>
  37
  383.使用浮点数
  39
  40<script language=javascript>
  41var num1,num2,num3,num4
  42num1=1234567890000.0
  43num2=5.14e23
  44num3=0.0000123456
  45num4=6.0254e3-4
  46document.write("浮点数1:"+num1+"<br>")
  47document.write("浮点数2:"+num2+"<br>")
  48document.write("浮点数3:"+num3+"<br>")
  49document.write("浮点数4:"+num4+"<br>")
  50</script>
  51
  524.使用布尔值
  53
  54<script language=javascript>
  55var answer1,answer2
  56answer1=true
  57answer2=false
  58document.write("显示布尔1:"+answer1+"<br>")
  59document.write("显示布尔2:"+answer2+"<br>")
  60</script>
  61
  625.使用字符串
  63
  64<script language=javascript>
  65var str1,str2
  66str1="fdsgdg dsfdsf china"
  67str2="武汉市广播电视大学"
  68document.write("显示字符串1:"+str1+"<br>")
  69document.write("显示字符串2:"+str2+"<br>")
  70</script>
  71
  726.确定变量类型
  73
  74<script>
  75var answer1,answer2,answer3,answer4
  76answer1=9
  77answer2=2.5
  78answer3="milky may"
  79answer4=true
  80document.write("变量1的类型是:"+typeof answer1 +"<br>")
  81document.write("变量2的类型是:"+typeof answer2 +"<br>")
  82document.write("变量3的类型是:"+typeof answer3 +"<br>")
  83document.write("变量4的类型是:"+typeof answer4 +"<br>")
  84</script>
  85
  867.将字符串转换成数字
  87
  88<script>
  89var str1="31 days in january"
  90var int1=parseInt(str1)
  91document.write("str1的数据类型是 :"+typeof str1+"<br>")
  92document.write("int1的数据类型是 :"+typeof int1+"<br>")
  93</script>
  94
  958.将数字转换成字符串
  96
  97<script>
  98var int1=256
  99var str1=""+int1
 100document.write("str1的数据类型是 :"+typeof str1+"<br>")
 101document.write("int1的数据类型是 :"+typeof int1+"<br>")
 102</script>
 103
 1049.声明数组
 105
 106<script>
 107array=new Array(5)
 108array[0]=1
 109array[1]=3
 110array[2]=5
 111array[3]=7
 112array[4]=11
 113document.write("数组是:"+array[0]+" "+array[1]+" "+array[2]+" "+array[3]+" "+array[4])
 114</script>
 115
 11610.确定数组元素的个数
 117
 118<script>
 119array=new Array(5)
 120array[0]=1
 121array[1]=3
 122array[2]=5
 123array[3]=7
 124array[4]=11
 125document.write("数组是:"+array[0]+" "+array[1]+" "+array[2]+" "+array[3]+" "+array[4]+"<br>")
 126document.write("数组的元素个数是"+array.length)
 127</script>
 128
 12911.将数组转换为字符串
 130
 131<script>
 132array=new Array()
 133array[0]="dark"
 134array[1]="apple"
 135array[2]="nebula"
 136array[3]="water"
 137str1=array.join()
 138str2=array.join(" ")
 139document.write(str1+"<br>")
 140document.write(str2)
 141</script>
 142
 14312.对数组排序
 144
 145<script>
 146array=new Array()
 147array[0]="dark"
 148array[1]="apple"
 149array[2]="nebula"
 150array[3]="water"
 151str1=array.sort()
 152document.write(str1+"<br>")
 153</script>
 154
 155第三章 创建表达式
 156
 1571.使用算术运算符
 158
 159<script>
 160var1=12
 161var2=10
 162varadd=var1+var2
 163varsub=var1-var2
 164varmult=var1*var2
 165vardiv=var1/var2
 166varmod=var1%var2
 167document.write("数据1是:"+var1+"<br>")
 168document.write("数据2是:"+var2+"<br>")
 169document.write("数据相加是:"+varadd+"<br>")
 170document.write("数据相减是:"+varsub+"<br>")
 171document.write("数据相乘是:"+varmult+"<br>")
 172document.write("数据相除是:"+vardiv+"<br>")
 173document.write("数据相除取余数是:"+varmod+"<br>")
 174</script>
 175
 1762.递增变量和递减变量
 177
 178<script>
 179days=1
 180document.write("输出变量"+days+"<br>")
 181days++
 182document.write("递增后变量变为:"+days)
 183</script>
 184
 1853.创建比较表达式
 186
 187<script>
 188daysofmonth=28
 189if(daysofmonth==28)
 190month="february"
 191document.write("days of month:"+daysofmonth+"<br>")
 192document.write("month:"+month)
 193</script>
 194
 1954.创建逻辑表达式
 196
 197<script>
 198dayofmonth=28
 199if(dayofmonth==28 || dayofmonth==29)
 200month="february"
 201document.write("days of month:"+dayofmonth+"<br>")
 202document.write("month:"+month)
 203</script>
 204
 2055.使用条件运算符
 206
 207<script language="javascript">
 208stomach="hungry";
 209time="5:00";
 210(stomach=="hungry"&&time=="5:00"? eat = "dinner":eat="a snack";
 211document.write("输出结果"+eat);
 212</script>
 213
 2146.识别数字
 215
 216<script>
 217var1=24;
 218(isNaN(var1))?document.write("变量var1"+var1+"不是数字"):Document.write("变量var1"+var1+"是数字")
 219</script>
 220
 221第四章 控制程序流程
 222
 2231.使用IF –Else语句
 224
 225<script>
 226month="december"
 227date=25
 228if(month=="december" && date==25)
 229document.write("今天是圣诞节,商店关门")
 230else
 231document.write("欢迎,您来商店购物")
 232</script>
 233
 2342.使用for 循环
 235
 236<script>
 237for (count=1;count<=10;count++)
 238document.write("输出第"+count+""+"<br>")
 239</script>
 240
 2413.使用while循环
 242
 243<script>
 244count=1
 245while(count<=15){
 246document.write("输出第"+count+"" +"<br>")
 247count++}

 248</script>
 249
 2504.中断循环
 251
 252<script>
 253count=1
 254while(count<=15){
 255count++
 256if(count==8)
 257break;
 258document.write("输出第"+count+""+"<br>")}

 259</script>
 260
 2615.继续循环
 262
 263<script>
 264count=1
 265while(count<=15){
 266count++
 267if(count==8)
 268continue;
 269document.write("输出第"+count+""+"<br>")}

 270</script>
 271
 2726.使用javascript定时器
 273
 274<script>
 275function rabbit()
 276{document.write("输出语句")
 277}

 278</script>
 279<body onload=window.setTimeout(rabbit(),5000)>
 280
 2817.设置定期间隔
 282
 283<script>
 284window.setInterval("document.form1.text2.value=document.form1.text1.value",3000)
 285</script>
 286<form name=form1>
 287<input type=text name=text1><br>
 288<input type=text name=text2><br>
 289</form>
 290
 2918.清除超时和间隔
 292
 293<script>
 294stop=window.setInterval("document.form1.text2.value=document.form1.text1.value",300)
 295</script>
 296<form name=form1>
 297<input type=text name=text1><br>
 298<input type=text name=text2><br>
 299<input type=button name=button1 value=" 清除超时和间隔" onclick=clearInterval(stop)>
 300</form>
 301
 302第五章 使用函数
 303
 3041.声明函数
 305
 306<script>
 307function quote()
 308{ document.write("输出语句")
 309}

 310</script>
 311
 3122.调用函数
 313
 314<script>
 315function quote()
 316{ document.write("输出语句")
 317}

 318quote()
 319</script>
 320
 3213.了解全局变量和局部变量
 322
 323任何不用 var关键字声明的变量都是全局变量,任何在函数外声明的变量都是全局变量
 324
 3254.将参数传送给函数
 326
 327<script>
 328function f(item)
 329{document.write("输出参数"+item+"<br>")
 330}

 331f("fgdfgd")
 332f("参数二")
 333</script>
 334
 3355.从函数返回值
 336
 337<script>
 338function average(var1,var2,var3)
 339{ave=(var1+var2+var3)/3;
 340document.write("输出结果");
 341return ave;
 342}

 343document.write(average(34,56,78))
 344</script>
 345
 3466.通过HTML链接调用函数
 347
 348<script>
 349function quote(){
 350document.write(" 输出字符串")
 351}

 352</script>
 353<a href=javascript:quote()>通过HTML链接调用函数</a>
 354<a href=javascript:Document.write("输出字符")> 通过HTML链接调用函数,直接写javascript语句</a>
 355
 356第六章 处理事件
 357
 3581.检查鼠标单击
 359
 360<form name=form1>
 361<input type=button name=button1 value=hello onclick=document.form1.button1.value='there'>
 362</form>
 363
 3642.检测双击
 365
 366<form name=form1>
 367<input type=button name=button1 value=hello onclick=document.form1.button1.value='你单击了按钮' ondblclick=document.form1.button1.value='你双击了该按钮'>
 368</form>
 369
 3703.创建悬停按钮
 371
 372<img src=go.gif onmouseover=document.images[0].src='go2.gif' onmouseout= document.images[0].src='go.gif'>
 373
 3744.检测按键
 375
 376<form name=form1>
 377<input type=text name=text1 value=hello onkeypress="if(window.event.keyCode=='100') document.form1.text1.value='你按了d键'">
 378</form>
 379
 3805.设置焦点
 381
 382<form name=form1>
 383<input type=text name=text1 value=hello
 384onfous=document.form1.text1.value='该文本框获得焦点'
 385onblur=document.form1.text1.value='该文本框失去焦点'>
 386</form>
 387
 3886.检测下拉菜单选择
 389
 390<form name=form1>
 391<select name=select1 size=4
 392onChange=document.form1.text1.value=document.form1.select1.value>
 393<option value="北京">北京</option>
 394<option value="上海">上海</option>
 395<option value="武汉">武汉</option>
 396<option value="天津">天津</option>
 397<option value="大连">大连</option>
 398</select>
 399<input tppe=text name=text1 value=hello>
 400</form>
 401
 4027.创建网页加载和卸载信息
 403
 404<body onload=document.form1.text1.value='页面加载完毕' onunload=alert('再见,欢迎再来')>
 405<form name=form1>
 406<input type=text name=text1 value="页面正在加载 ……">
 407</form>
 408
 409第七章 使用对象
 410
 4111.理解对象\属性和方法
 412
 413<body bgcolor="green">
 414<script>
 415document.write("页面背景颜色是:"+document.bgColor)
 416document.write("页面前景颜色是:"+document.fgColor)
 417</script>
 418
 4192.使用网页元素对象
 420
 421<script>
 422</script>
 423<form name=form1>
 424<textarea name=ta1>dfgfdgfdhfdhdfdfgdf</textarea>
 425<input type=button value="选择文本" onclick=document.form1.ta1.select()>
 426<input type=button value="显示文本" onclick=document.write(document.form1.ta1.value)>
 427</form>
 428
 4293.使用子对象
 430
 431
 432<form name=form1>
 433<input type=text name=text1 value=hello>
 434</form>
 435<script>
 436document.form1.text1.value="gdfgfd"
 437</script>
 438
 439<form name=form1>
 440<input type=radio name=radio1>
 441<input type=radio name=radio2>
 442</script>
 443<script>
 444document.form1.radio1.checked=true
 445</script>
 446
 4474.使用预定义对象
 448
 449<script>
 450str1="dgdfgdfgdfhf固定法固定法功夫攻打法"
 451document.write(str1+"<br>")
 452str2=str1.substr(5)
 453document.write(str2+"<br>")
 454document.write("输出圆的面积:"+Math.PI*Math.pow(5.0,2))
 455</script>
 456
 4575.创建新对象
 458
 459<script>
 460today=new Date()
 461document.write("今天是"+(today.getMonth()+1)+""+today.getDate()+""+"<br>")
 462document.write("现在是:"+today.toLocaleString())
 463</script>
 464
 4656.引用当前对象
 466
 467<form name=form1>
 468<input type=text name=text1 value="dgdgdfgfd" onclick=this.select()>
 469</script>
 470
 4717.查看对象属性
 472
 473<script>
 474for(prop in window)
 475{document.write("window."+prop+"="+window[prop]+"<br>");}
 476for(prop2 in location)
 477{document.write("location."+prop2+"="+location[prop]+"<br>");}
 478</script>
 479
 4808.使用Array对象
 481
 482<script>
 483array=new Array(10)
 484array[0]="bark"
 485array[1]="apple"
 486array[2]="nebula"
 487array[3]="cookie"
 488array[4]="technology"
 489document.write("数组元素个数是"+array.Length+"<br>")
 490document.write("用 join将数组合并"+array.join(" ")+"<br>")
 491document.write(" 数组排序"+array.sort())
 492</script>
 493
 4949.使用 image 对象
 495
 496<img src=**.gif alt="图片提示…." border=10>
 497<script>
 498document.write("图片提示是:"+document.images[0].alt+"<br>")
 499document.write("图片边框大小是:"+document.images[0].broder)
 500</script>
 501
 50210.预加载图像
 503
 504<script>
 505freddy=new Image()
 506freddy.src=freddy.gif
 507</script>
 508<body onload=document.images[0].src=freddy.src>
 509,<img src="blank.gif">
 510</body>
 511
 51211.改变图像
 513
 514<img src=freddy.gif><br>
 515<form name=form1>
 516<input type=button name=button1 value="改变图像" onclickd=document.images[0].src=dudjp.gif>
 517</form>
 518
 51912.使用link和anchor对象
 520
 521<a name=anchor1>锚点1<br>
 522<a href=http://www.microsoft.com>Microsoft</a><br>
 523<a href=http://www.sohu.com>sohu</a><br>
 524<a href=http://www.sina.com.cn>sina</a><br>
 525<script>
 526document.write("本页面共有"+document.links.length+"链接"+"<br>")
 527document.write("本页面共有"+document.anchors.length+"锚点"+"<br>")
 528document.write("第一个链接协议是"+document.links[0].protocol+"<br>")
 529document.write("第一个链接路径是"+document.links[0].pathnamel+"<br>")
 530document.write("第一个链接href是"+document.links[0].hrefl+"<br>")
 531</script>
 532
 53313.改变链接
 534
 535<a href =http://www.microsoft.com>link</a>
 536<form name=form1>
 537<input type=button name=button1 value="改变链接" onclick=document.links[0].href='http://www.sohu.com'>
 538</form>
 539
 54014.使用history对象
 541
 542<form name=form1>
 543<input type=button name=button1 value="向后返回2页" onclick=window.history.go(-2)>
 544</form>
 545
 546第八章 使用窗口
 547
 5481.在浏览器的状态栏上显示文本
 549
 550<body onload=window.status="欢迎光临我的站点">
 551<a href=http://www.sohu.com>sohu</a>
 552</body>
 553
 5542.改变背景色
 555
 556<script>
 557document.bgColor="orange"
 558</script>
 559
 5603.列举背景颜色
 561
 562<body bgColor =green>
 563<script>
 564document.write("当前背景色是:"+document.bgColor)
 565</script>
 566</body>
 567
 5684.改变文本和链接颜色
 569
 570<script>
 571document.bgColor="orange"
 572document.fgColor="blue"
 573document.linkColor="red"
 574</script>
 575<h2>看看这段文本颜色</h2>
 576<a href=http://www.sohu.com>sohu</a>
 577</body>
 578
 5795.改变文档标题
 580
 581<script>
 582name="Mouse"
 583document.title="welcome to "+name+"'s House"
 584document.write(document.title)
 585</script>
 586
 5876.显示修改日期
 588
 589<script>
 590document.write("本页面最后修改时间是"+document.lastModified)
 591</script>
 592
 5937.查看当前文档的URL
 594
 595<script>
 596document.write("本页面的URL:"+document.URL)
 597</script>
 598
 5998.查看引用页
 600
 601<script>
 602document.write("本页面的引用页是"+document.referrer)
 603</script>
 604
 6059.打开新的浏览器窗口
 606
 607<script>
 608window.open("*.htm","title","width=200,height=400,resizable=yes")
 609</script>
 610
 61110.关闭远程窗口
 612
 613
 614close.html:
 615<script>
 616document.write("正文")
 617</script>
 618<form name=form1>
 619<input type=button name=button1value="关闭" onclick=window.close()>
 620</form>
 621
 622open.html
 623<script>
 624window.open("close.html","romote","width=200,height=400,resizable=yes")
 625</script>
 626
 62711.打印窗口
 628
 629<script>
 630document.write("正文")
 631</script>
 632<form name=form1>
 633<input type=button value=打印 onclick=window.print()>
 634</form>
 635
 63612.移动窗口
 637
 638
 639<form name=form1>
 640水平方向<input type=text name=x value=20>
 641垂直方向<input type=text name=y value=50>
 642<input type=button value="移动窗口到…"onclick=window.moveTo(document.form1.x.value,document.form1.y.value)>
 643</form>
 644
 645<form name=form1>
 646水平方向<input type=text name=x value=20>
 647垂直方向<input type=text name=y value=50>
 648<input type=button value="移动窗口"onclick=window.moveBy(document.form1.x.value,document.form1.y.value)>
 649</form>
 650
 651
 65213.改变窗口大小
 653
 654
 655<form name=form1>
 656水平方向<input type=text name=x value=200>
 657垂直方向<input type=text name=y value=500>
 658<input type=button value="改变窗口大小到….."onclick=window.resizeTo(document.form1.x.value,document.form1.y.value)>
 659</form>
 660
 661<form name=form1>
 662水平方向<input type=text name=x value=200>
 663垂直方向<input type=text name=y value=500>
 664<input type=button value="改变窗口大小"onclick=window.resizeBy(document.form1.x.value,document.form1.y.value)>
 665</form>
 666
 66714.用警告对话框通知用户
 668
 669<script>
 670window.alert("welcome")
 671</script>
 672
 67315.用提示对话框接受输入
 674
 675<script>
 676name=window.prompt("输入姓名","姓名")
 677document.write(" 欢迎您:"+name+"来到这里")
 678</script>
 679
 68016.用确认对话框使用户做出决定
 681
 682<script>
 683like=window.confirm("你觉得好吗?")
 684if(like==true)
 685document.write("谢谢你的夸奖")
 686else
 687document.write("希望得到你的夸奖")
 688</script>
 689
 690第九章 使用字符串
 691
 6921.使用字符串对象
 693
 694<script>
 695mystring="gdgdfgfddddaaaaaaaaaaaabbbbbbbbbbbbbbbbbvbhg.<br>"
 696document.write(mystring)
 697document.write(mystring.bold())
 698document.write(mystring.toUpperCase())
 699</script>
 700
 7012.使用子字符串
 702
 703<script>
 704str1="fdsf 1111 gfdgfd dfdsf cccc dddd.<br>"
 705document.write(str1)
 706document.write(str1.substring(0,13)+"<br>")
 707document.write(str1.substr (20,11)+"<br>")
 708</script>
 709
 7103.连接字符串
 711
 712<script>
 713str1="may you find"
 714str2="peace,happiness and prosperity.<br>"
 715document.write(str1+"<br>")
 716document.write(str2)
 717document.write(str1.concat(str2))
 718document.write(str1+=str2)
 719</script>
 720
 7214.格式化字符串变量
 722
 723<script>
 724str1="peace,happiness and prosperity.<br>"
 725document.write(str1)
 726document.write(str1.big())
 727document.write(str1.small())
 728document.write(str1.bold())
 729document.write(str1.italics())
 730document.write(str1.strike())
 731document.write(str1.fontsize(6))
 732document.write(str1.fontcolor(green))
 733</script>
 734
 7355.创建锚和链接
 736
 737<script>
 738str1="this is the bigginning of the page.<br>"
 739str2="….<br>"
 740str3="this is the end of the page .<br>"
 741str4="link to the start<br>"
 742str5="link to the end<br>"
 743document.write(str1.anchor("start"))
 744for(i=0;i<10;i++)
 745document.write(str2);
 746document.write(str3.anchor("end"))
 747document.write(str4.link("#start"))
 748document.write(str5.link("#end"))
 749</script>
 750
 7516.确定字符串长度
 752
 753<script>
 754str1="this is the bigginning of the page."
 755document.write(str1+"<br>")
 756document.write( "字符串的长度是:"+str1.length)
 757document.write("字符串全部大写是;"+str1.toUpperCase())
 758document.write("字符串全部小写是;"+str1.toLowerCase())
 759</script>
 760
 7617.在字符串内搜索
 762
 763<script>
 764str1="this is the end of the line.<br>"
 765document.write(str1)
 766document.write("字符end在字符串的位置是"+str1.search("end"))
 767document.write("字符dog在字符串的位置是"+str1.search("dog"))
 768</script>
 769
 7708.定位字符串中的字符
 771
 772<script>
 773str1="spring is a time for flowers and trees and baby bunnles<br>"
 774document.write(str1)
 775document.write("the index for the second word ‘and' is"+str1.indexOf("and",30))
 776documednt.write("the last index of the word ‘and' is "+str1.lastIndexOf("and"))
 777</script>
 778
 7799.替换字符串中的文本
 780
 781<script>
 782str1="spring is a time for flowers and trees and baby bunnles<br>"
 783document.write(str1)
 784document .write(str1.replace("and",","))
 785</script>
 786
 78710.字符串分离
 788
 789<script>
 790str1="spring is a time for flowers and trees and baby bunnles<br>"
 791document.write(str1)
 792str1array=str1.split(" ")
 793document.write(str1array[0]+"<br>")
 794document.write(str1array[1]+"<br>")
 795document.write(str1array[2]+"<br>")
 796document.write(str1array[3]+"<br>")
 797</script>
 798
 799第十章 使用日期和时间
 800
 8011.使用Date对象
 802
 803<script>
 804cdate=new Date("august 2,1989 12:30:00")
 805document.write(cdate)
 806</script>
 807
 8082.显示当地时间和日期
 809
 810<script>
 811cdate=new Date()
 812document.write("当前时间是:"+cdate.toGMTString()+"<br>")
 813document.write("日期和时间是:"+cdate.toLocaleString())
 814</script>
 815
 8163.获得时间和日期值
 817
 818<script>
 819cdate=new Date()
 820document.write("显示当前的星期"+cdate.getDay()+"<br>")
 821document.write("显示当前的月份"+cdate.getMonth()+"<br>")
 822document.write("显示当前的日期"+cdate.getDay()+"<br>")
 823document.write("显示当前的年份"+cdate.getYear()+"<br>")
 824document.write("显示当前的小时"+cdate.getHours()+"<br>")
 825document.write("显示当前的分钟"+cdate.getMinutes()+"<br>")
 826document.write("显示当前的秒"+cdate.getSeconds()+"<br>")
 827</script>
 828
 8294.设置时间和日期值
 830
 831<script language=javascript>
 832cdate=new Date("December 25,1984")
 833document.write("显示日期"+cdate+"<br>")
 834document.write("设置月份"+cdate.setMonth(10)+"<br>")
 835document.write("设置日期"+cdate.setDate(23)+"<br>")
 836document.write("设置年份"+cdate.setYear(2000)+"<br>")
 837document.write("设置小时"+cdate.setHours(13)+"<br>");
 838document.write("设置分钟"+cdate.setMinutes(47)+"<br>");
 839document.write("设置秒"+cdate.setSeconds(23)+"<br>");
 840document.write("显示设置后的日期和时间"+cdate);
 841</script>
 842
 843第十一章 使用Math对象
 844
 8451. 使用Math对象
 846
 847<script language=javascript>
 848</script>
 849<form name=form1>
 850圆的半径:<input type=text name=rad><br>
 851圆的面积:<input type=text name=area><br>
 852<input type=button name=button1 value=计算圆的面积 onclick=document.form1.area.value=document.form1.rad.value*document.
 853form1.rad.value*Math.PI>
 854</form>
 855
 8562.生成随机数
 857
 858<script>
 859array1=new Array(
 860"这是第1句",
 861"这是第2句",
 862"这是第3句",
 863"这是第4句",
 864"这是第5句",
 865"这是第6句")
 866RandomNo=Math.floor(array1.length*Math.random())
 867document.write("随机输出某一句"+"<br>"+array1[RandomNo])
 868</script>
 869
 8703.使用平方根
 871
 872<form name=form1>
 873value:<input type=text name=va1><br>
 874平方根<input type=text name=sqrt><br>
 875<input type=button name=button1 value=计算平方根
 876onclick="document.form1.sqrt.value=Math.sqrt(document.form1.va1.value)">
 877</form>
 878
 8794.数字的舍入
 880
 881<form name=form1>
 882输入<input type=text name=val><br>
 883舍入的结果<input type=text name=round><br>
 884<input type=button name=button1 value=计算结果 onclick=document.form1.round.value=Math.round(document.form1.val.value)>
 885</form>
 886
 8875.乘方运算
 888
 889<form name=form1>
 890底数<input type=text name=val><br>
 891指数<input type=text name=power><br>
 892<input type=text name=result><br>
 893<input type=button name=button1 value=计算结果 onclick="document.form1.result.value=Math.pow (document.form1.val.value,document.form1.power.value)">
 894</form>
 895
 8966.发现最小值和最大值
 897
 898<form name=form1>
 899数字1<input type=text name=val1><br>
 900数字2<input type=text name=val2><br>
 901最小值<input type=text name=min><br>
 902最大值<input type=text name=max><br>
 903数字1<input type=button value=计算 onclick="document.form1.min.value=Math.min (document.form1.val1.value,document.form1.val2.value);document.form1.
 904max.value= Math.max(document.form1.val1.value,document.form1.val2.value)">
 905</form>
 906
 907第十二章 使用表单
 908
 9091.使用文本框
 910
 911
 912<form name=form1>
 913<input type=text value="information ,please"name=text1>
 914</form>
 915<script>
 916document.write("表单text1类型是: "+document.form1.text1.type+"<br>")
 917document.write("表单text1名称是: "+document.form1.text1.name+"<br>")
 918document.write("表单text1值是: "+document.form1.text1.value+"<br>")
 919document.write("表单text1大小是: "+document.form1.text1.size+"<br>")
 920</script>
 921
 922<form name=form1>
 923<input type=text name=text1 value=click here
 924onfocus=document.form1.text1.select()>
 925</form>
 926
 9272.使用密码框
 928
 929<form name=form1>
 930<input type=password name=pw1 value=daylight>
 931</form>
 932<script>
 933document.write("表单pw1的类型:"+document.form1.pw1.type+"<br>")
 934document.write("表单pw1的名称:"+document.form1.pw1.name+"<br>")
 935document.write("表单pw1的值:"+document.form1.pw1.value+"<br>")
 936document.write("表单pw1的大小:"+document.form1.pw1.size+"<br>")
 937</script>
 938
 9393.使用隐藏字段
 940
 941<form name=form1>
 942<input type=hidden name=hid1 value=piece of eight>
 943</form>
 944<script>
 945document.write("表单hid1的类型:"+document.form1.hid1.type+"<br>")
 946document.write("表单hid1的名称:"+document.form1.hid1.name+"<br>")
 947document.write("表单hid1的值:"+document.form1.hid1.value+"<br>")
 948</script>
 949
 9504.使用文本区域框
 951
 952
 953<form name=form1>
 954<textarea name=ta1>how many grains of sand are there in the sahara desert?</textarea>
 955</form>
 956<script>
 957document.write("表单ta1的类型:"+document.form1.ta1.type+"<br>")
 958document.write("表单ta1的名称:"+document.form1.ta1.name+"<br>")
 959document.write("表单ta1的值:"+document.form1.ta1.value+"<br>")
 960document.write("表单ta1的横向宽度:"+document.form1.ta1.cols+"<br>")
 961document.write("表单ta1的纵向宽度:"+document.form1.rows.value+"<br>")
 962</script>
 963
 9646.使用重置按钮
 965
 966<form name=form1>
 967<input type=reset name=reset1 value="rest form">
 968</form>
 969<script>
 970document.write("表单reset1的类型:"+document.form1.reset1.type+"<br>")
 971document.write("表单reset1的名称:"+document.form1.reset1.name+"<br>")
 972document.write("表单reset1的值:"+document.form1.reset1.value+"<br>")
 973</script>
 974
 9757.使用提交按钮
 976
 977<form name=form1>
 978<input type=submit name=submit1 value="submit form">
 979</form>
 980<script>
 981document.write("表单submit1的类型:"+document.form1.submit1.type+"<br>")
 982document.write("表单submit1的名称:"+document.form1.submit1.name+"<br>")
 983document.write("表单submit1的值:"+document.form1.submit1.value+"<br>")
 984</script>
 985
 9868.使用复选按钮
 987
 988<form name=form1>
 989<input type=checkbox name=cb1 >computer savvy?
 990</form>
 991<script>
 992document.write("表单cb1的类型:"+document.form1.cb1.type+"<br>")
 993document.write("表单cb1是否被选择?:"+document.form1.cb1.checked+"<br>")
 994document.write("表单cb1的名称:"+document.form1.cb1.name+"<br>")
 995</script>
 996
 9979.使用单选按钮
 998
 999<form name=form1>
1000<input type=radio name=radio1>male
1001<input type=radio name=radio1>female
1002</form>
1003<script>
1004document.write("第一个按钮被选择"+document.form1.radio1[0].checked+"<br>")
1005document.write("第二个按钮被选择"+document.form1.radio1[1].checked+"<br>")
1006document.write("按钮的名称"+ document.form1.radio1[0].name+"<br>")
1007document.write("按钮的个数"+document.form1.radio1.length)
1008</script>
1009
101010.使用选择列表
1011
1012<form name=form1>
1013<select name=select1 size=4>
1014<option name=option1 value=lon>london,England</option>
1015<option name=option2 value=dub>Dublin,Ireland</option>
1016</select>
1017</form>
1018<script>
1019document.write("这个选择列表的名称"+document.form1.select1.name+"<br>")
1020document.write("这个选择列表的长度"+document.form1.select1.length+"<br>")
1021document.write("这个选择列表当前被选择的索引号"+document.form1.select1.selectedIndex+"<br>")
1022document.write("这个选择列表的尺寸"+document.form1.select1.size+"<br>")
1023</script>
1024
102511.验证表单的有效性
1026
1027<script>
1028function validate(){
1029if(document.form1.text1.value!='1'||'2'||'3'||'4'){
1030alert("请输入1~4的整数")
1031}

1032}

1033</script>
1034<form name=form1>
1035请输入1~4的整数:
1036<input type=text name=text1 size=4 onchange=validate()>
1037</form>
1038
103912.控制表单焦点
1040
1041<form name=form1>
1042<input type=text name=text1 value=where is you focus?><br>
1043<input type=text name=text2 value=is there?><br>
1044<input type=text name=text3 value=or maybe here?><br>
1045<input type=button name=button1 value="text box #1" onclick=document.form1.text1.focus()><br>
1046<input type=button name=button2 value="text box #2" onclick=document.form1.text2.focus()><br>
1047<input type=button name=button3 value="text box #3" onclick=document.form1.text3.focus()><br>
1048</form>
1049
1050第十三章 使用分栏
1051
1052第十四章 使用navigator
1053
10541.使用navigator对象
1055
1056<script>
1057document.write("navigator对象的属性"+"<br>")
1058document.write("appcodename:"+navigator.appCodeName+"<br>")
1059document.write("appname::"+navigator.appName+"<br>")
1060document.write("appversion:"+navigator.appVersion+"<br>")
1061document.write("platform:"+navigator.platform+"<br>")
1062document.write("userAgent:"+navigator.userAgent+"<br>")
1063</script>
1064<script>
1065document.write("navigator对象的方法"+"<br>")
1066document.write("javaEnabled():"+navigator.javaEnabled())
1067</script>
1068
10692.检查用户的浏览器
1070
1071<script>
1072if(navigator.appName.indexOf("Microsoft")!=-1){
1073document.write("用户浏览器是微软的IE浏览器"+"<br>")}

1074else if(navigator.appName.indexOf("Netscape")!=-1){
1075document.write("用户浏览器是netscape的netscape浏览器"+"<br>")}

1076if(navigator.appVersion.indexOf("4.0")!=-1){
1077document.write("you are using a version 4.0compatible browser")
1078}

1079else{
1080document.write("this browser is not 4.0 compliant")}

1081</script>
1082
10833.检测用户的操作系统
1084
1085<script>
1086if (navigator.platform.indexOf("win32")!=-1){
1087document.write("you are using a computer running windows 95 or highter")}

1088else{
1089document.write("this computer is not running windows 95 or higher")}

1090</script>
1091
10924.使用location对象
1093
1094
1095<script>
1096document.write("location对象的属性"+"<br>")
1097document.write("hash"+location.hash+"<br>")
1098document.write("hostname"+location.hostname+"<br>")
1099document.write("host"+location.host+"<br>")
1100document.write("href"+location.href+"<br>")
1101document.write("port"+location.port+"<br>")
1102document.write("search"+location.search+"<br>")
1103</script>
1104
1105重新加载网页
1106<form name=form1>
1107<input type=button name=button1 value=重新加载本页 onclick=location.reload>
1108</form>
1109
1110
11115.使用cookie
1112
1113
1114<script>
1115finction makecookie(){
1116if(!document.cookie){
1117name=prompt("请输入你的姓名");
1118document.cookie="name="+name+";";}

1119}

1120</script>
1121
1122<body onload=makecookie()>
1123<script>
1124function makecookie(){
1125if(!document.cookie){
1126name=prompt("请输入你的姓名")
1127document.cookie="name="+name+";";
1128namestart=document.cookie.indexOf("="); 
1129nameend=document.cookieindexOf(";");
1130document.writeln("your name is:"+document.cookie.substring(namestart+1,nameend)+",br>")
1131}

1132}

1133</script>
1134
posted @ 2006-12-25 11:39  Lixy  阅读(308)  评论(0)    收藏  举报