javascript基礎知識整理_基礎知識

來源:互聯網
上載者:User

1 建立指令碼塊
程式碼

複製代碼 代碼如下:

<script language="JavaScript">
JavaScript 代碼寫在這裡面
</script>

2 隱藏指令碼代碼
程式碼
<script language="JavaScript"> <!- document.write("Hello"); //-> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

在不支援JavaScript的瀏覽器中將不執行相關代碼
3 瀏覽器不支援的時候顯示
程式碼
複製代碼 代碼如下:

<noscript>
Hello to the non-JavaScript browser.
</noscript>

4 連結外部指令檔
程式碼
複製代碼 代碼如下:

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

5 注釋指令碼
程式碼
複製代碼 代碼如下:

// This is a comment
document.write("Hello"); // This is a comment
/*
All of this
is a comment
*/

6 輸出到瀏覽器
程式碼
複製代碼 代碼如下:

document.write("<strong>Hello jb51 .net</strong>");

7 定義變數
程式碼
複製代碼 代碼如下:

var myVariable = "some value";

8 字串相加
程式碼
複製代碼 代碼如下:

var myString = "String1" + "String2";

9 字串搜尋
程式碼
<script language="JavaScript"> <!- var myVariable = "Hello there"; var therePlace = myVariable.search("there"); document.write(therePlace); // -> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

10 字串替換
程式碼
複製代碼 代碼如下:

thisVar.replace("Monday","Friday");

11 格式化字串
程式碼
<script language="JavaScript"> <!- var myVariable = "Hello there"; document.write(myVariable.big() + ""); document.write(myVariable.blink() + ""); document.write(myVariable.bold() + ""); document.write(myVariable.fixed() + ""); document.write(myVariable.fontcolor("red") + ""); document.write(myVariable.fontsize("18pt") + ""); document.write(myVariable.italics() + ""); document.write(myVariable.small() + ""); document.write(myVariable.strike() + ""); document.write(myVariable.sub() + ""); document.write(myVariable.sup() + ""); document.write(myVariable.toLowerCase() + ""); document.write(myVariable.toUpperCase() + ""); var firstString = "My String"; var finalString = firstString.bold().toLowerCase().fontcolor("red"); // -> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

12 建立數組
程式碼
<script language="JavaScript"> <!- var myArray = new Array(5); myArray[0] = "First Entry"; myArray[1] = "Second Entry"; myArray[2] = "Third Entry"; myArray[3] = "Fourth Entry"; myArray[4] = "Fifth Entry"; var anotherArray = new Array("First Entry","Second Entry","Third Entry","Fourth Entry","Fifth Entry"); // -> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

13 數組排序
程式碼
<script language="JavaScript"> <!- var myArray = new Array(5); myArray[0] = "z"; myArray[1] = "c"; myArray[2] = "d"; myArray[3] = "a"; myArray[4] = "q"; document.write(myArray.sort()); // -> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

14 分割字串
程式碼
<script language="JavaScript"> <!- var myVariable = "a,b,c,d"; var stringArray = myVariable.split(","); document.write(stringArray[0]); document.write(stringArray[1]); document.write(stringArray[2]); document.write(stringArray[3]); // -> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

15 彈出警告資訊
程式碼
<script language="JavaScript"> <!- window.alert("Hello"); // -> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

16 彈出確認框
程式碼
<script language="JavaScript"> <!- var result = window.confirm("Click OK to continue"); // -> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

17 自訂函數
程式碼
<script language="JavaScript"> <!- function multiple(number1,number2) { var result = number1 * number2; return result; } // -> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

18 調用JS函數
程式碼
複製代碼 代碼如下:

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

19 在頁面載入完成後執行函數
程式碼
複製代碼 代碼如下:

<body onLoad="functionName();">
Body of the page
</body>

20 條件判斷
程式碼
<script> <!- var userChoice = window.confirm("Choose OK or Cancel"); var result = (userChoice == true) ? "OK" : "Cancel"; document.write(result); // -> </script>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

21 指定次數迴圈
程式碼
<script> <!- var myArray = new Array(3); myArray[0] = "Item 0"; myArray[1] = "Item 1"; myArray[2] = "Item 2"; for (i = 0; i < myArray.length; i++) { document.write(myArray[i] + ""); } // ->
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.