亚洲欧美成人综合一区_国产精品一区二区无码_亚洲风情偷拍区_成?人免费无码视频在线看

在線客服與您一對一交流

JavaScript方法和技巧大全

趁著節(jié)日的空隙,把有關(guān)JavaScript的方法和技巧整理下,讓每個在為JavaScript而煩惱的人明白,JavaScript就這么回事!并希望JavaScript還可以成為你的朋友,讓你豁然開朗,在項目中更好的應(yīng)用~

適合閱讀范圍:對JavaScript一無所知~離精通只差一步之遙的人
基礎(chǔ)知識:HTML


JavaScript就這么回事1:基礎(chǔ)知識

1 創(chuàng)建腳本塊

1: <script language=”JavaScript”>
2: JavaScript code goes here
3: </script>

 

2 隱藏腳本代碼

1: <script language=”JavaScript”>
2: <!--
3: document.write(“Hello”);
4: // -->
5: </script>


在不支持JavaScript的瀏覽器中將不執(zhí)行相關(guān)代碼

3 瀏覽器不支持的時候顯示

1: <noscript>
2: Hello to the non-JavaScript browser.
3: </noscript>

 

4 鏈接外部腳本文件

1: <script language=”JavaScript” src="/”filename.js"”></script>


5 注釋腳本

1: // This is a comment
2: document.write(“Hello”); // This is a comment
3: /*
4: All of this
5: is a comment
6: */

 

6 輸出到瀏覽器

1: document.write(“<strong>Hello</strong>”);

 

7 定義變量

1: var myVariable = “some value”;

 

8 字符串相加

1: var myString = “String1” + “String2”;

 

9 字符串搜索

1: <script language=”JavaScript”>
2: <!--
3: var myVariable = “Hello there”;
4: var therePlace = myVariable.search(“there”);
5: document.write(therePlace);
6: // -->
7: </script>

 

10 字符串替換

1: thisVar.replace(“Monday”,”Friday”);


11 格式化字串

1: <script language=”JavaScript”>
2: <!--
3: var myVariable = “Hello there”;
4: document.write(myVariable.big() + “<br>”);
5: document.write(myVariable.blink() + “<br>”);
6: document.write(myVariable.bold() + “<br>”);
7: document.write(myVariable.fixed() + “<br>”);
8: document.write(myVariable.fontcolor(“red”) + “<br>”);
9: document.write(myVariable.fontsize(“18pt”) + “<br>”);
10: document.write(myVariable.italics() + “<br>”);
11: document.write(myVariable.small() + “<br>”);
12: document.write(myVariable.strike() + “<br>”);
13: document.write(myVariable.sub() + “<br>”);
14: document.write(myVariable.sup() + “<br>”);
15: document.write(myVariable.toLowerCase() + “<br>”);
16: document.write(myVariable.toUpperCase() + “<br>”);
17:
18: var firstString = “My String”;
19: var finalString = firstString.bold().toLowerCase().fontcolor(“red”);
20: // -->
21: </script>

 

12 創(chuàng)建數(shù)組

1: <script language=”JavaScript”>
2: <!--
3: var myArray = new Array(5);
4: myArray[0] = “First Entry”;
5: myArray[1] = “Second Entry”;
6: myArray[2] = “Third Entry”;
7: myArray[3] = “Fourth Entry”;
8: myArray[4] = “Fifth Entry”;
9: var anotherArray = new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”);
10: // -->
11: </script>

 

13 數(shù)組排序

1: <script language=”JavaScript”>
2: <!--
3: var myArray = new Array(5);
4: myArray[0] = “z”;
5: myArray[1] = “c”;
6: myArray[2] = “d”;
7: myArray[3] = “a”;
8: myArray[4] = “q”;
9: document.write(myArray.sort());
10: // -->
11: </script>

 

14 分割字符串

1: <script language=”JavaScript”>
2: <!--
3: var myVariable = “a,b,c,d”;
4: var stringArray = myVariable.split(“,”);
5: document.write(stringArray[0]);
6: document.write(stringArray[1]);
7: document.write(stringArray[2]);
8: document.write(stringArray[3]);
9: // -->
10: </script>

 

15 彈出警告信息

1: <script language=”JavaScript”>
2: <!--
3: window.alert(“Hello”);
4: // -->
5: </script>

 

16 彈出確認框

1: <script language=”JavaScript”>
2: <!--
3: var result = window.confirm(“Click OK to continue”);
4: // -->
5: </script>

 

17 定義函數(shù)

1: <script language=”JavaScript”>
2: <!--
3: function multiple(number1,number2) {
4: var result = number1 * number2;
5: return result;
6: }
7: // -->
8: </script>

 

18 調(diào)用JS函數(shù)

1: <a href=”#” onClick=”functionName()”>Link text</a>
2: <a href="/”javascript:functionName"()”>Link text</a>

 

19 在頁面加載完成后執(zhí)行函數(shù)

1: <body onLoad=”functionName();”>
2: Body of the page
3: </body>


20 條件判斷

1: <script>
2: <!--
3: var userChoice = window.confirm(“Choose OK or Cancel”);
4: var result = (userChoice == true) ? “OK” : “Cancel”;
5: document.write(result);
6: // -->
7: </script>

 

21 指定次數(shù)循環(huán)

1: <script>
2: <!--
3: var myArray = new Array(3);
4: myArray[0] = “Item 0”;
5: myArray[1] = “Item 1”;
6: myArray[2] = “Item 2”;
7: for (i = 0; i < myArray.length; i++) {
8: document.write(myArray[i] + “<br>”);
9: }
10: // -->
11: </script>

 

22 設(shè)定將來執(zhí)行

1: <script>
2: <!--
3: function hello() {
4: window.alert(“Hello”);
5: }
6: window.setTimeout(“hello()”,5000);
7: // -->
8: </script>

 

23 定時執(zhí)行函數(shù)

1: <script>
2: <!--
3: function hello() {
4: window.alert(“Hello”);
5: window.setTimeout(“hello()”,5000);
6: }
7: window.setTimeout(“hello()”,5000);
8: // -->
9: </script>

 

24 取消定時執(zhí)行

1: <script>
2: <!--
3: function hello() {
4: window.alert(“Hello”);
5: }
6: var myTimeout = window.setTimeout(“hello()”,5000);
7: window.clearTimeout(myTimeout);
8: // -->
9: </script>

 

25 在頁面卸載時候執(zhí)行函數(shù)

1: <body onUnload=”functionName();”>
2: Body of the page
3: </body>

JavaScript就這么回事2:瀏覽器輸出


26 訪問document對象

1: <script language=”JavaScript”>
2: var myURL = document.URL;
3: window.alert(myURL);
4: </script>

 

27 動態(tài)輸出HTML

1: <script language=”JavaScript”>
2: document.write(“<p>Here’s some information about this document:</p>”);
3: document.write(“<ul>”);
4: document.write(“<li>Referring Document: “ + document.referrer + “</li>”);
5: document.write(“<li>Domain: “ + document.domain + “</li>”);
6: document.write(“<li>URL: “ + document.URL + “</li>”);
7: document.write(“</ul>”);
8: </script>


28 輸出換行

1: document.writeln(“<strong>a</strong>”);
2: document.writeln(“b”);

 

29 輸出日期

1: <script language=”JavaScript”>
2: var thisDate = new Date();
3: document.write(thisDate.toString());
4: </script>

 

30 指定日期的時區(qū)

1: <script language=”JavaScript”>
2: var myOffset = -2;
3: var currentDate = new Date();
4: var userOffset = currentDate.getTimezoneOffset()/60;
5: var timeZoneDifference = userOffset - myOffset;
6: currentDate.setHours(currentDate.getHours() + timeZoneDifference);
7: document.write(“The time and date in Central Europe is: “ + currentDate.toLocaleString());
8: </script>


31 設(shè)置日期輸出格式

1: <script language=”JavaScript”>
2: var thisDate = new Date();
3: var thisTimeString = thisDate.getHours() + “:” + thisDate.getMinutes();
4: var thisDateString = thisDate.getFullYear() + “/” + thisDate.getMonth() + “/” + thisDate.getDate();
5: document.write(thisTimeString + “ on “ + thisDateString);
6: </script>


32 讀取URL參數(shù)

1: <script language=”JavaScript”>
2: var urlParts = document.URL.split(“?”);
3: var parameterParts = urlParts[1].split(“&”);
4: for (i = 0; i < parameterParts.length; i++) {
5: var pairParts = parameterParts[i].split(“=”);
6: var pairName = pairParts[0];
7: var pairValue = pairParts[1];
8: document.write(pairName + “ :“ +pairValue );
9: }
10: </script>

你還以為HTML是無狀態(tài)的么?

33 打開一個新的document對象

1: <script language=”JavaScript”>
2: function newDocument() {
3: document.open();
4: document.write(“<p>This is a New Document.</p>”);
5: document.close();
6: }
7: </script>

 

34 頁面跳轉(zhuǎn)

1: <script language=”JavaScript”>
2: window.location = “http://www.liu21st.com/”;
3: </script>

 

35 添加網(wǎng)頁加載進度窗口

1: <html>
2: <head>
3: <script language='javaScript'>
4: var placeHolder = window.open('holder.html','placeholder','width=200,height=200');
5: </script>
6: <title>The Main Page</title>
7: </head>
8: <body onLoad='placeHolder.close()'>
9: <p>This is the main page</p>
10: </body>
11: </html>

 

JavaScript就這么回事3:圖像

 

36 讀取圖像屬性

1: <img src="/”image1.jpg"” name=”myImage”>
2: <a href=”# ” onClick=”window.alert(document.myImage.width)”>Width</a>
3:


37 動態(tài)加載圖像

1: <script language=”JavaScript”>
2: myImage = new Image;
3: myImage.src = “Tellers1.jpg”;
4: </script>


38 簡單的圖像替換

1: <script language=”JavaScript”>
2: rollImage = new Image;
3: rollImage.src = “rollImage1.jpg”;
4: defaultImage = new Image;
5: defaultImage.src = “image1.jpg”;
6: </script>
7: <a href="/”myUrl"” onMouSEOver=”document.myImage.src = rollImage.src;”
8: onMouseOut=”document.myImage.src = defaultImage.src;”>
9: <img src="/”image1.jpg"” name=”myImage” width=100 height=100 border=0>


39 隨機顯示圖像

1: <script language=”JavaScript”>
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 imageChoice = Math.floor(Math.random() * imageList.length);
8: document.write(‘<img src=”’ + imageList[imageChoice] + ‘“>’);
9: </script>


40 函數(shù)實現(xiàn)的圖像替換

1: <script language=”JavaScript”>
2: var source = 0;
3: var replacement = 1;
4: function createRollOver(originalImage,replacementImage) {
5: var imageArray = new Array;
6: imageArray[source] = new Image;
7: imageArray[source].src = originalImage;
8: imageArray[replacement] = new Image;
9: imageArray[replacement].src = replacementImage;
10: return imageArray;
11: }
12: var rollImage1 = createRollOver(“image1.jpg”,”rollImage1.jpg”);
13: </script>
14: <a href=”#” onMouseOver=”document.myImage1.src = rollImage1[replacement].src;”
15: onMouseOut=”document.myImage1.src = rollImage1[source].src;”>
16: <img src="/”image1.jpg"” width=100 name=”myImage1” border=0>
17: </a>


41 創(chuàng)建幻燈片

1: <script language=”JavaScript”>
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: </script>
19: </head>
20: <body onLoad=”slideShow(0)”>
21: <img src="/”image1.jpg"” width=100 name=”slideShow”>


42 隨機廣告圖片

1: <script language=”JavaScript”>
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: </script>

西安蟠龍網(wǎng)絡(luò)科技有限公司:西北地區(qū)的網(wǎng)站運營商!

相關(guān)文章:

  • 色彩是光的魔術(shù),所有的色彩都是經(jīng)過光的謀劃而呈現(xiàn)出來的。這些經(jīng)過光的魔法演變出來的顏色則構(gòu)成了我們這個斑斕的萬千世界。西安做網(wǎng)站推薦閱讀 西安網(wǎng)站建設(shè):網(wǎng)站改版不可忽視的...

  • 相信每一位站長在做網(wǎng)站推廣的時候,都會用到兩個神器,一個是站長之家上面的站長工具,一個是百度站長平臺。對于好多站長來說,做網(wǎng)站推廣其實就是在做百度推廣,想要做好百...

  • 每個站長都希望自己建好的網(wǎng)站可以運行的良好,帶來可觀的收益,那么優(yōu)秀的網(wǎng)站建設(shè)流程就必須要完整才可以做出優(yōu)秀的網(wǎng)站。西安網(wǎng)站建設(shè)推薦閱讀 西安網(wǎng)站建設(shè):網(wǎng)站制作之欄...

  • 告訴你幾個關(guān)于網(wǎng)站設(shè)計的小訣竅,讓你設(shè)計起來不再難:西安網(wǎng)站建設(shè)推薦閱讀 西安網(wǎng)站建設(shè):企業(yè)網(wǎng)站建設(shè)前必須知道的域名知識 1.你能掌控的時間 據(jù)調(diào)察,一個人瀏覽一個完整...

  • 文章是網(wǎng)站建設(shè)優(yōu)化中比較重要的任務(wù),文章的好壞直接影響著網(wǎng)站的排名,網(wǎng)站也是靠著文章才有發(fā)展的那一天,那么怎么寫出好的、高質(zhì)量的文章就尤為重要了。西安網(wǎng)站建設(shè)推薦...

  • 現(xiàn)在,大多數(shù)的企業(yè)都有網(wǎng)站,但是對于做網(wǎng)站需要注意哪些原則問題呢?這是很多站長都容易忽略的。像很多店家都在注重網(wǎng)站的建設(shè),但是是不是經(jīng)常改版就好呢?是不是網(wǎng)站越漂...

  • 網(wǎng)站是企業(yè)營銷的主要組成部分,可以為企業(yè)帶來更多的潛在客戶,創(chuàng)造更多的價值。中小企業(yè)基本上都有自己的網(wǎng)站,但仍有很多企業(yè)沒有認識到網(wǎng)上推廣營銷的重要性。西安網(wǎng)站建...

  • 很多的客戶都很麻木,不知道做個網(wǎng)站要了解些什么知識,通常在網(wǎng)站看到如何建立一個網(wǎng)站的相關(guān)提問,下面劍鋒網(wǎng)絡(luò)針對網(wǎng)站建設(shè)基礎(chǔ)知識跟大家分享一下。 西安網(wǎng)站建設(shè) 其實這...

  • 在當下,網(wǎng)站對于企業(yè)來說,似乎成了必須品。在互聯(lián)網(wǎng)中,大大小小的網(wǎng)站已經(jīng)有了上億個,但不是每個網(wǎng)站都能達到他原來想要達到的要求。在網(wǎng)站建設(shè)中,總是會遇到大大小小的...

  • 隨著互聯(lián)網(wǎng)+時代的到來,大多數(shù)傳統(tǒng)企業(yè)產(chǎn)生了嚴重的危機感,隨著信息經(jīng)濟繁榮催化新的理念變革和模式創(chuàng)新,互聯(lián)網(wǎng)+時代不僅外部的行業(yè)規(guī)則面臨深刻變革和顛覆,內(nèi)部的企業(yè)管...

  • 公司:西安蟠龍網(wǎng)絡(luò)科技有限公司
  • 聯(lián)系人:張經(jīng)理
  • 手機/微信:
  • Q Q: 點擊這里給我發(fā)消息
  • 地址:西安市雁塔區(qū)唐延南路11號逸翠園i都會