JavaScript使用方法和技巧(三)

來源:互聯網
上載者:User

41 建立投影片

1: <s cript language=”Javas cript”>
2: var imageList = new Array;
3: imageList[0] = new Image;
4: imageList[0].src = “image1.jpg”;
5: imageList[1] = new Image;
6: imageList[1].src = “image2.jpg”;
7: imageList[2] = new Image;
8: imageList[2].src = “image3.jpg”;
9: imageList[3] = new Image;
10: imageList[3].src = “image4.jpg”;
11: function slideShow(imageNumber) {
12: document.slideShow.src = imageList[imageNumber].src;
13: imageNumber += 1;
14: if (imageNumber < imageList.length) {
15: window.setTimeout(“slideShow(“ + imageNumber + “)”,3000);
16: }
17: }
18: </s cript>
19: </head>
20: <body onLoad=”slideShow(0)”>
21: <img src="/”image1.jpg"” width=100 name=”slideShow”>

42 隨機廣告圖片

1: <s cript language=”Javas cript”>
2: var imageList = new Array;
3: imageList[0] = “image1.jpg”;
4: imageList[1] = “image2.jpg”;
5: imageList[2] = “image3.jpg”;
6: imageList[3] = “image4.jpg”;
7: var urlList = new Array;
8: urlList[0] = http://some.host/”;
9: urlList[1] = http://another.host/”;
10: urlList[2] = http://somewhere.else/”;
11: urlList[3] = http://right.here/”;
12: var imageChoice = Math.floor(Math.random() * imageList.length);
13: document.write(‘<a href=”’ + urlList[imageChoice] + ‘“><img src=”’ + imageList[imageChoice] + ‘“></a>’);
14: </s cript>

Javas cript就這麼回事4:表單

還是先繼續寫完JS就這麼回事系列吧~
43 表單構成

1: <form method=”post” action=”target.html” name=”thisForm”>
2: <input type=”text” name=”myText”>
3: <select name=”mySelect”>
4: <option value=”1”>First Choice</option>
5: <option value=”2”>Second Choice</option>
6: </select>
7: <br/>
8: <input type=”submit” value=”Submit Me”>
9: </form>

44 訪問表單中的文字框內容

1: <form name=”myForm”>
2: <input type=”text” name=”myText”>
3: </form>
4: <a href='#' onClick='window.alert(document.myForm.myText.value);'>Check Text Field</a>

45 動態複製文字框內容

1: <form name=”myForm”>
2: Enter some Text: <input type=”text” name=”myText”><br/>
3: Copy Text: <input type=”text” name=”copyText”>
4: </form>
5: <a href=”#” onClick=”document.myForm.copyText.value =
6: document.myForm.myText.value;”>Copy Text Field</a>

46 偵測文字框的變化

1: <form name=”myForm”>
2: Enter some Text: <input type=”text” name=”myText” onChange=”alert(this.value);”>
3: </form>

47 訪問選中的Select

1: <form name=”myForm”>
2: <select name=”mySelect”>
3: <option value=”First Choice”>1</option>
4: <option value=”Second Choice”>2</option>
5: <option value=”Third Choice”>3</option>
6: </select>
7: </form>
8: <a href='#' onClick='alert(document.myForm.mySelect.value);'>Check Selection List</a>

48 動態增加Select項

1: <form name=”myForm”>
2: <select name=”mySelect”>
3: <option value=”First Choice”>1</option>
4: <option value=”Second Choice”>2</option>
5: </select>
6: </form>
7: <s cript language=”Javas cript”>
8: document.myForm.mySelect.length++;
9: document.myForm.mySelect.options[document.myForm.mySelect.length - 1].text = “3”;
10: document.myForm.mySelect.options[document.myForm.mySelect.length - 1].value = “Third Choice”;
11: </s cript>

49 驗證表單欄位

1: <s cript language=”Javas cript”>
2: function checkField(field) {
3: if (field.value == “”) {
4: window.alert(“You must enter a value in the field”);
5: field.focus();
6: }
7: }
8: </s cript>
9: <form name=”myForm” action=”target.html”>
10: Text Field: <input type=”text” name=”myField”onBlur=”checkField(this)”>
11: <br/><input type=”submit”>
12: </form>

50 驗證Select項

1: function checkList(selection) {
2: if (selection.length == 0) {
3: window.alert(“You must make a selection from the list.”);
4: return false;
5: }
6: return true;
7: }

51 動態改變表單的action

1: <form name=”myForm” action=”login.html”>
2: Username: <input type=”text” name=”username”><br/>
3: Password: <input type=”password” name=”password”><br/>
4: <input type=”button” value=”Login” onClick=”this.form.submit();”>
5: <input type=”button” value=”Register” onClick=”this.form.action = ‘register.html’; this.form.submit();”>
6: <input type=”button” value=”Retrieve Password” onClick=”this.form.action = ‘password.html’; this.form.submit();”>
7: </form>

52 使用映像按鈕

1: <form name=”myForm” action=”login.html”>
2: Username: <input type=”text” name=”username”><br/>
3: Password: <input type=”password”name=”password”><br/>
4: <input type=”image” src="/”login.gif"” value=”Login”>
5: </form>
6:

53 表單資料的加密

1: <s cript LANGUAGE='Javas cript'>
2: <!--
3: function encrypt(item) {
4: var newItem = '';
5: for (i=0; i < item.length; i++) {
6: newItem += item.charCodeAt(i) + '.';
7: }
8: return newItem;
9: }
10: function encryptForm(myForm) {
11: for (i=0; i < myForm.elements.length; i++) {
12: myForm.elements[i].value = encrypt(myForm.elements[i].value);
13: }
14: }
15:
16: //-->
17: </s cript>
18: <form name='myForm' onSubmit='encryptForm(this); window.alert(this.myField.value);'>
19: Enter Some Text: <input type=text name=myField><input type=submit>
20: </form>

 

Javas cript就這麼回事5:視窗和架構

54 改變瀏覽器狀態列文字提示

1: <s cript language=”Javas cript”>
2: window.status = “A new status message”;
3: </s cript>

55 彈出確認提示框

1: <s cript language=”Javas cript”>
2: var userChoice = window.confirm(“Click OK or Cancel”);
3: if (userChoice) {
4: document.write(“You chose OK”);
5: } else {
6: document.write(“You chose Cancel”);
7: }
8: </s cript>

56 提示輸入

1: <s cript language=”Javas cript”>
2: var userName = window.prompt(“Please Enter Your Name”,”Enter Your Name Here”);
3: document.write(“Your Name is “ + userName);
4: </s cript>

57 開啟一個新視窗

1: //開啟一個名稱為myNewWindow的瀏覽器新視窗
2: <s cript language=”Javas cript”>
3: window.open(http://www.liu21st.com/”,”myNewWindow”);
4: </s cript>

58 設定新視窗的大小

1: <s cript language=”Javas cript”>
2: window.open(http://www.liu21st.com/”,”myNewWindow”,'height=300,width=300');
3: </s cript>

59 設定新視窗的位置

1: <s cript language=”Javas cript”>
2: window.open(http://www.liu21st.com/”,”myNewWindow”,'height=300,width=300,left=200,screenX=200,top=100,screenY=100');
3: </s cript>

60 是否顯示工具列和滾動欄

1: <s cript language=”Javas cript”>
2: window.open(“http:

61 是否可以縮放新視窗的大小

1: <s cript language=”Javas cript”>
2: window.open(http://www.liu21st.com/' , 'myNewWindow', 'resizable=yes' );</s cript>

62 載入一個新的文檔到當前視窗

1: <a href='#' onClick='document.location = '125a.html';' >Open New Document</a>

63 設定頁面的滾動位置

1: <s cript language=”Javas cript”>
2: if (document.all) { //如果是IE瀏覽器則使用scrollTop屬性
3: document.body.scrollTop = 200;
4: } else { //如果是NetScape瀏覽器則使用pageYOffset屬性
5: window.pageYOffset = 200;
6: }</s cript>

64 在IE中開啟全屏視窗

1: <a href='#' onClick="window.open(http://www.juxta.com/','newWindow','fullScreen=yes');">Open a full-screen window</a>

65 新視窗和父視窗的操作

1: <s cript language=”Javas cript”>
2: //定義新視窗
3: var newWindow = window.open(“128a.html”,”newWindow”);
4: newWindow.close(); //在父視窗中關閉開啟的新視窗
5: </s cript>
6: 在新視窗中關閉父視窗
7: window.opener.close()

66 往新視窗中寫內容

1: <s cript language=”Javas cript”>
2: var newWindow = window.open(“”,”newWindow”);
3: newWindow.document.open();
4: newWindow.document.write(“This is a new window”);
5: newWIndow.document.close();
6: </s cript>

67 載入頁面到架構頁面

1: <frameset cols=”50%,*”>
2: <frame name=”frame1” src="/”135a.html"”>
3: <frame name=”frame2” src="/”about:blank"”>
4: </frameset>
5: 在frame1中載入frame2中的頁面
6: parent.frame2.document.location = “135b.html”;

68 在架構頁面之間共用指令碼
如果在frame1中html檔案中有個指令碼

1: function doAlert() {
2: window.alert(“Frame 1 is loaded”);
3: }

那麼在frame2中可以如此調用該方法

1: <body onLoad=”parent.frame1.doAlert();”>
2: This is frame 2.
3: </body>

69 資料公用
可以在架構頁面定義資料項目,使得該資料可以被多個架構中的頁面公用

1: <s cript language=”Javas cript”>
2: var persistentVariable = “This is a persistent value”;
3: </s cript>
4: <frameset cols=”50%,*”>
5: <frame name=”frame1” src="/”138a.html"”>
6: <frame name=”frame2” src="/”138b.html"”>
7: </frameset>

這樣在frame1和frame2中都可以使用變數persistentVariable
70 架構程式碼程式庫
根據以上的一些思路,我們可以使用一個隱藏的架構頁面來作為整個框架組的程式碼程式庫

1: <frameset cols=”0,50%,*”>
2: <frame name=”codeFrame” src="/”140code.html"”>
3: <frame name=”frame1” src="/”140a.html"”>
4: <frame name=”frame2” src="/”140b.html"”>
5: </frameset>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.