javascript
javascript基础整理
1 創建腳本塊
引用內容程序代碼
<script language=”JavaScript”>
JavaScript 代碼寫在這里面
</script>
2 隱藏腳本代碼
引用內容程序代碼
<script language=”JavaScript”>
<!–
document.write(“Hello”);
// –>
</script>
在不支持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</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>
10 字符串替換
引用內容程序代碼
thisVar.replace(“Monday”,”Friday”);
11 格式化字串
引用內容程序代碼
<script language=”JavaScript”>
<!–
var myVariable = “Hello there”;
document.write(myVariable.big() + “<br>”);
document.write(myVariable.blink() + “<br>”);
document.write(myVariable.bold() + “<br>”);
document.write(myVariable.fixed() + “<br>”);
document.write(myVariable.fontcolor(“red”) + “<br>”);
document.write(myVariable.fontsize(“18pt”) + “<br>”);
document.write(myVariable.italics() + “<br>”);
document.write(myVariable.small() + “<br>”);
document.write(myVariable.strike() + “<br>”);
document.write(myVariable.sub() + “<br>”);
document.write(myVariable.sup() + “<br>”);
document.write(myVariable.toLowerCase() + “<br>”);
document.write(myVariable.toUpperCase() + “<br>”);
var firstString = “My String”;
var finalString = firstString.bold().toLowerCase().fontcolor(“red”);
// –>
</script>
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>
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>
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>
15 彈出警告信息
引用內容程序代碼
<script language=”JavaScript”>
<!–
window.alert(“Hello”);
// –>
</script>
16 彈出確認框
引用內容程序代碼
<script language=”JavaScript”>
<!–
var result = window.confirm(“Click OK to continue”);
// –>
</script>
17 自定義函數
引用內容程序代碼
<script language=”JavaScript”>
<!–
function multiple(number1,number2) {
var result = number1 * number2;
return result;
}
// –>
</script>
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>
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] + “<br>”);
}
// –>
轉載于:https://www.cnblogs.com/wangjisi/archive/2010/06/03/1750877.html
總結
以上是生活随笔為你收集整理的javascript基础整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NHibernateLinq简单的CRU
- 下一篇: 开发网站用什么编码