现在的位置: 首页 > 综合 > 正文

JavaScript基本语法

2013年04月21日 ⁄ 综合 ⁄ 共 3570字 ⁄ 字号 评论关闭
  1. 创建脚本块
  2. <script language=”JavaScript”>
  3. JavaScript code goes here
  4. </script>
  5. 隐藏脚本代码
  6. <script language=”JavaScript”>
  7. <!--
  8. document.write(“Hello”);
  9. // -->
  10. </script>
  11. 浏览器不支持的时候显示
  12. <noscript>
  13. Hello to the non-JavaScript browser.
  14. </noscript>
  15. 链接外部脚本文件
  16. <script language=”JavaScript” src="/”youname.js"”></script>
  17. 注释脚本
  18. // This is a comment
  19. document.write(“Hello”); // This is a comment
  20. /*
  21. All of this
  22. is a comment
  23. */  
  24. 输出到浏览器
  25. document.write(“<strong>输出内容</strong>”);
  26. 定义变量
  27. var myVariable = “some value”;
  28. 字符串相加
  29. var myString = “String1” + “String2”;
  30. 字符串搜索
  31. <script language=”JavaScript”>
  32. <!--
  33. var myVariable = “Hello there”;
  34. var therePlace = myVariable.search(“there”);
  35. document.write(therePlace);
  36. // -->
  37. </script>
  38. 字符串替换
  39. thisVar.replace(“Monday”,”Friday”);
  40. 格式化字串
  41. <script language=”JavaScript”>
  42. <!--
  43. var myVariable = “Hello there”;
  44. document.write(myVariable.big() + “<br>”);
  45. document.write(myVariable.blink() + “<br>”);
  46. document.write(myVariable.bold() + “<br>”);
  47. document.write(myVariable.fixed() + “<br>”);
  48. document.write(myVariable.fontcolor(“red”) + “<br>”);
  49. document.write(myVariable.fontsize(“18pt”) + “<br>”);
  50. document.write(myVariable.italics() + “<br>”);
  51. document.write(myVariable.small() + “<br>”);
  52. document.write(myVariable.strike() + “<br>”);
  53. document.write(myVariable.sub() + “<br>”);
  54. document.write(myVariable.sup() + “<br>”);
  55. document.write(myVariable.toLowerCase() + “<br>”);
  56. document.write(myVariable.toUpperCase() + “<br>”);
  57. var firstString = “My String”;
  58. var finalString = firstString.bold().toLowerCase().fontcolor(“red”);
  59. // -->
  60. </script>
  61. 创建数组
  62. <script language=”JavaScript”>
  63. <!--
  64. var myArray = new Array(5);
  65. myArray[0] = “First Entry”;
  66. myArray[1] = “Second Entry”;
  67. myArray[2] = “Third Entry”;
  68. myArray[3] = “Fourth Entry”;
  69. myArray[4] = “Fifth Entry”;
  70. var anotherArray = new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”);
  71. // -->
  72. </script>
  73. 数组排序
  74. <script language=”JavaScript”>
  75. <!--
  76. var myArray = new Array(5);
  77. myArray[0] = “z”;
  78. myArray[1] = “c”;
  79. myArray[2] = “d”;
  80. myArray[3] = “a”;
  81. myArray[4] = “q”;
  82. document.write(myArray.sort());
  83. // -->
  84. </script>
  85. 分割字符串
  86. <script language=”JavaScript”>
  87. <!--
  88. var myVariable = “a,b,c,d”;
  89. var stringArray = myVariable.split(“,”);
  90. document.write(stringArray[0]);
  91. document.write(stringArray[1]);
  92. document.write(stringArray[2]);
  93. document.write(stringArray[3]);
  94. // -->
  95. </script>
  96. 弹出警告信息
  97. <script language=”JavaScript”>
  98. <!--
  99. window.alert(“Hello”);
  100. // -->
  101. </script>
  102. 弹出确认框
  103. <script language=”JavaScript”>
  104. <!--
  105. var result = window.confirm(“Click OK to continue”);
  106. // -->
  107. </script>  
  108. 定义函数
  109. <script language=”JavaScript”>
  110. <!--
  111. function multiple(number1,number2) {
  112. var result = number1 * number2;
  113. return result;
  114. }
  115. // -->
  116. </script>  
  117. 调用JS函数
  118. <a href=”#” onClick=”functionName()”>Link text</a>
  119. <a href="/”javascript:functionName"()”>Link text</a>  
  120. 在页面加载完成后执行函数
  121. <body onLoad=”functionName();”>
  122. Body of the page
  123. </body>
  124. 条件判断
  125. <script>
  126. <!--
  127. var userChoice = window.confirm(“Choose OK or Cancel”);
  128. var result = (userChoice == true) ? “OK” : “Cancel”;
  129. document.write(result);
  130. // -->
  131. </script>
  132. 指定次数循环
  133. <script>
  134. <!--
  135. var myArray = new Array(3);
  136. myArray[0] = “Item 0”;
  137. myArray[1] = “Item 1”;
  138. myArray[2] = “Item 2”;
  139. for (i = 0; i < myArray.length; i++) {
  140. document.write(myArray + “<br>”);
  141. }
  142. // -->
  143. </script>
  144. 设定将来执行
  145. <script>
  146. <!--
  147. function hello() {
  148. window.alert(“Hello”);
  149. }
  150. window.setTimeout(“hello()”,5000);
  151. // -->
  152. </script>
  153. 定时执行函数
  154. <script>
  155. <!--
  156. function hello() {
  157. window.alert(“Hello”);
  158. window.setTimeout(“hello()”,5000);
  159. }
  160. window.setTimeout(“hello()”,5000);
  161. // -->
  162. </script>
  163. 取消定时执行
  164. <script>
  165. <!--
  166. function hello() {
  167. window.alert(“Hello”);
  168. }
  169. var myTimeout = window.setTimeout(“hello()”,5000);
  170. window.clearTimeout(myTimeout);
  171. // -->
  172. </script>
  173. 在页面卸载时候执行函数
  174. <body onUnload=”functionName();”>
  175. Body of the page
  176. </body> 

抱歉!评论已关闭.