JavaScript方法和技巧大全——基础知识(2)
2010-09-14 13:40:05 来源:WEB开发网格式化字串
1<script language=”JavaScript”>
2 var myVariable = “Hello there”;
3 document.write(myVariable.big() + “<br>”);
4 document.write(myVariable.blink() + “<br>”);
5 document.write(myVariable.bold() + “<br>”);
6 document.write(myVariable.fixed() + “<br>”);
7 document.write(myVariable.fontcolor(“red”) + “<br>”);
8 document.write(myVariable.fontsize(“18pt”) + “<br>”);
9 document.write(myVariable.italics() + “<br>”);
10 document.write(myVariable.small() + “<br>”);
11 document.write(myVariable.strike() + “<br>”);
12 document.write(myVariable.sub() + “<br>”);
13 document.write(myVariable.sup() + “<br>”);
14 document.write(myVariable.toLowerCase() + “<br>”);
15 document.write(myVariable.toUpperCase() + “<br>”);
16 var firstString = “My String”;
17 var finalString = firstString.bold().toLowerCase().fontcolor(“red”);
18</script>
创建数组
1<script language=”JavaScript”>
2 var myArray = new Array(5);
3 myArray[0] = “First Entry”;
4 myArray[1] = “Second Entry”;
5 myArray[2] = “Third Entry”;
6 myArray[3] = “Fourth Entry”;
7 myArray[4] = “Fifth Entry”;
8 var anotherArray = new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”);
9</script>
数组排序
1<script language=”JavaScript”>
2 var myArray = new Array(5);
3 myArray[0] = “z”;
4 myArray[1] = “c”;
5 myArray[2] = “d”;
6 myArray[3] = “a”;
7 myArray[4] = “q”;
8 document.write(myArray.sort());
9</script>
分割字符串
1<script language=”JavaScript”>
2 var myVariable = “a,b,c,d”;
3 var stringArray = myVariable.split(“,”);
4 document.write(stringArray[0]);
5 document.write(stringArray[1]);
6 document.write(stringArray[2]);
7 document.write(stringArray[3]);
8</script>
弹出警告信息
1<script language=”JavaScript”>
2 window.alert(“Hello”);
3</script>
弹出确认框
1<script language=”JavaScript”>
2 var result = window.confirm(“Click OK to continue”);
3</script>
定义函数
1<script language=”JavaScript”>
2 function multiple(number1,number2) {
3 var result = number1 * number2;
4 return result;
5 }
6</script>
调用JS函数
1<a href=”#” onClick=”functionName()”>Link text</a>
2<a href="/”javascript:functionName"()”>Link text</a>
在页面加载完成后执行函数
1<body onLoad=”functionName();”>
2Body of the page
3</body>
条件判断
1<script>
2 var userChoice = window.confirm(“Choose OK or Cancel”);
3 var result = (userChoice == true) ? “OK” : “Cancel”;
4 document.write(result);
5</script>
Tags:JavaScript 方法 技巧
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接