且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JavaScript基本语法

更新时间:2022-09-11 08:56:06

 


  1. 创建脚本块   
  2. <scriptlanguage=”JavaScript”>   
  3. JavaScriptcodegoeshere   
  4. </script>   
  5. 隐藏脚本代码   
  6.  
  7. <scriptlanguage=”JavaScript”>   
  8. <!--   
  9. document.write(“Hello”);   
  10. //-->   
  11. </script>   
  12.  
  13. 浏览器不支持的时候显示   
  14.  
  15. <noscript>   
  16. Hellotothenon-JavaScriptbrowser.   
  17. </noscript>   
  18.  
  19. 链接外部脚本文件   
  20.  
  21. <scriptlanguage=”JavaScript”src="/”youname.js"”></script>   
  22.  
  23. 注释脚本   
  24.  
  25. //Thisisacomment   
  26. document.write(“Hello”);//Thisisacomment   
  27. /*   
  28. Allofthis   
  29. isacomment   
  30. */   
  31.  
  32. 输出到浏览器   
  33.  
  34. document.write(“<strong>输出内容</strong>”);   
  35.  
  36. 定义变量   
  37.  
  38. varmyVariable=“somevalue”;   
  39.  
  40. 字符串相加   
  41.  
  42. varmyString=“String1”+“String2”;   
  43.  
  44. 字符串搜索   
  45.  
  46. <scriptlanguage=”JavaScript”>   
  47. <!--   
  48. varmyVariable=“Hellothere”;   
  49. vartherePlace=myVariable.search(“there”);   
  50. document.write(therePlace);   
  51. //-->   
  52. </script>   
  53.  
  54. 字符串替换   
  55.  
  56. thisVar.replace(“Monday”,”Friday”);   
  57.  
  58. 格式化字串   
  59.  
  60. <scriptlanguage=”JavaScript”>   
  61. <!--   
  62. varmyVariable=“Hellothere”;   
  63. document.write(myVariable.big()+“<br>”);   
  64. document.write(myVariable.blink()+“<br>”);   
  65. document.write(myVariable.bold()+“<br>”);   
  66. document.write(myVariable.fixed()+“<br>”);   
  67. document.write(myVariable.fontcolor(“red”)+“<br>”);   
  68. document.write(myVariable.fontsize(“18pt”)+“<br>”);   
  69. document.write(myVariable.italics()+“<br>”);   
  70. document.write(myVariable.small()+“<br>”);   
  71. document.write(myVariable.strike()+“<br>”);   
  72. document.write(myVariable.sub()+“<br>”);   
  73. document.write(myVariable.sup()+“<br>”);   
  74. document.write(myVariable.toLowerCase()+“<br>”);   
  75. document.write(myVariable.toUpperCase()+“<br>”);   
  76. varfirstString=“MyString”;   
  77. varfinalString=firstString.bold().toLowerCase().fontcolor(“red”);   
  78. //-->   
  79. </script>   
  80.  
  81. 创建数组   
  82.  
  83. <scriptlanguage=”JavaScript”>   
  84. <!--   
  85. varmyArray=newArray(5);   
  86. myArray[0]=“FirstEntry”;   
  87. myArray[1]=“SecondEntry”;   
  88. myArray[2]=“ThirdEntry”;   
  89. myArray[3]=“FourthEntry”;   
  90. myArray[4]=“FifthEntry”;   
  91. varanotherArray=newArray(“FirstEntry”,”SecondEntry”,”ThirdEntry”,”FourthEntry”,”FifthEntry”);   
  92. //-->   
  93. </script>   
  94.  
  95. 数组排序   
  96.  
  97. <scriptlanguage=”JavaScript”>   
  98. <!--   
  99. varmyArray=newArray(5);   
  100. myArray[0]=“z”;   
  101. myArray[1]=“c”;   
  102. myArray[2]=“d”;   
  103. myArray[3]=“a”;   
  104. myArray[4]=“q”;   
  105. document.write(myArray.sort());   
  106. //-->   
  107. </script>   
  108.  
  109. 分割字符串   
  110.  
  111. <scriptlanguage=”JavaScript”>   
  112. <!--   
  113. varmyVariable=“a,b,c,d”;   
  114. varstringArray=myVariable.split(“,”);   
  115. document.write(stringArray[0]);   
  116. document.write(stringArray[1]);   
  117. document.write(stringArray[2]);   
  118. document.write(stringArray[3]);   
  119. //-->   
  120. </script>   
  121.  
  122. 弹出警告信息   
  123.  
  124. <scriptlanguage=”JavaScript”>   
  125. <!--   
  126. window.alert(“Hello”);   
  127. //-->   
  128. </script>   
  129.  
  130. 弹出确认框   
  131.  
  132. <scriptlanguage=”JavaScript”>   
  133. <!--   
  134. varresult=window.confirm(“ClickOKtocontinue”);   
  135. //-->   
  136. </script>   
  137.  
  138. 定义函数   
  139.  
  140. <scriptlanguage=”JavaScript”>   
  141. <!--   
  142. functionmultiple(number1,number2){   
  143. varresult=number1*number2;   
  144. returnresult;   
  145. }   
  146. //-->   
  147. </script>   
  148.  
  149. 调用JS函数   
  150.  
  151. <ahref=”#”onClick=”functionName()”>Linktext</a>   
  152. <ahref="/”javascript:functionName"()”>Linktext</a>   
  153.  
  154. 在页面加载完成后执行函数   
  155.  
  156. <bodyonLoad=”functionName();”>   
  157. Bodyofthepage   
  158. </body>   
  159.  
  160. 条件判断   
  161.  
  162. <script>   
  163. <!--   
  164. varuserChoice=window.confirm(“ChooseOKorCancel”);   
  165. varresult=(userChoice==true)?“OK”:“Cancel”;   
  166. document.write(result);   
  167. //-->   
  168. </script>   
  169.  
  170. 指定次数循环   
  171.  
  172. <script>   
  173. <!--   
  174. varmyArray=newArray(3);   
  175. myArray[0]=“Item0”;   
  176. myArray[1]=“Item1”;   
  177. myArray[2]=“Item2”;   
  178. for(i=0;i<myArray.length;i++){   
  179. document.write(myArray+“<br>”);   
  180. }   
  181. //-->   
  182. </script>   
  183.  
  184. 设定将来执行   
  185.  
  186. <script>   
  187. <!--   
  188. functionhello(){   
  189. window.alert(“Hello”);   
  190. }   
  191. window.setTimeout(“hello()”,5000);   
  192. //-->   
  193. </script>   
  194.  
  195. 定时执行函数   
  196.  
  197. <script>   
  198. <!--   
  199. functionhello(){   
  200. window.alert(“Hello”);   
  201. window.setTimeout(“hello()”,5000);   
  202. }   
  203. window.setTimeout(“hello()”,5000);   
  204. //-->   
  205. </script>   
  206.  
  207. 取消定时执行   
  208.  
  209. <script>   
  210. <!--   
  211. functionhello(){   
  212. window.alert(“Hello”);   
  213. }   
  214. varmyTimeout=window.setTimeout(“hello()”,5000);   
  215. window.clearTimeout(myTimeout);   
  216. //-->   
  217. </script>   
  218.  
  219. 在页面卸载时候执行函数   
  220. <bodyonUnload=”functionName();”>   
  221. Bodyofthepage   
  222. </body>  

 本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/380691,如需转载请自行联系原作者