JavaScript基本语法
2010-08-20 17:25
435 查看
创建脚本块 <scriptlanguage=”JavaScript”> JavaScriptcodegoeshere </script> 隐藏脚本代码 <scriptlanguage=”JavaScript”> <!-- document.write(“Hello”); //--> </script> 浏览器不支持的时候显示 <noscript> Hellotothenon-JavaScriptbrowser. </noscript> 链接外部脚本文件 <scriptlanguage=”JavaScript”src="/”youname.js"”></script> 注释脚本 //Thisisacomment document.write(“Hello”);//Thisisacomment /* Allofthis isacomment */ 输出到浏览器 document.write(“<strong>输出内容</strong>”); 定义变量 varmyVariable=“somevalue”; 字符串相加 varmyString=“String1”+“String2”; 字符串搜索 <scriptlanguage=”JavaScript”> <!-- varmyVariable=“Hellothere”; vartherePlace=myVariable.search(“there”); document.write(therePlace); //--> </script> 字符串替换 thisVar.replace(“Monday”,”Friday”); 格式化字串 <scriptlanguage=”JavaScript”> <!-- varmyVariable=“Hellothere”; 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>”); varfirstString=“MyString”; varfinalString=firstString.bold().toLowerCase().fontcolor(“red”); //--> </script> 创建数组 <scriptlanguage=”JavaScript”> <!-- varmyArray=newArray(5); myArray[0]=“FirstEntry”; myArray[1]=“SecondEntry”; myArray[2]=“ThirdEntry”; myArray[3]=“FourthEntry”; myArray[4]=“FifthEntry”; varanotherArray=newArray(“FirstEntry”,”SecondEntry”,”ThirdEntry”,”FourthEntry”,”FifthEntry”); //--> </script> 数组排序 <scriptlanguage=”JavaScript”> <!-- varmyArray=newArray(5); myArray[0]=“z”; myArray[1]=“c”; myArray[2]=“d”; myArray[3]=“a”; myArray[4]=“q”; document.write(myArray.sort()); //--> </script> 分割字符串 <scriptlanguage=”JavaScript”> <!-- varmyVariable=“a,b,c,d”; varstringArray=myVariable.split(“,”); document.write(stringArray[0]); document.write(stringArray[1]); document.write(stringArray[2]); document.write(stringArray[3]); //--> </script> 弹出警告信息 <scriptlanguage=”JavaScript”> <!-- window.alert(“Hello”); //--> </script> 弹出确认框 <scriptlanguage=”JavaScript”> <!-- varresult=window.confirm(“ClickOKtocontinue”); //--> </script> 定义函数 <scriptlanguage=”JavaScript”> <!-- functionmultiple(number1,number2){ varresult=number1*number2; returnresult; } //--> </script> 调用JS函数 <ahref=”#”onClick=”functionName()”>Linktext</a> <ahref="/”javascript:functionName"()”>Linktext</a> 在页面加载完成后执行函数 <bodyonLoad=”functionName();”> Bodyofthepage </body> 条件判断 <script> <!-- varuserChoice=window.confirm(“ChooseOKorCancel”); varresult=(userChoice==true)?“OK”:“Cancel”; document.write(result); //--> </script> 指定次数循环 <script> <!-- varmyArray=newArray(3); myArray[0]=“Item0”; myArray[1]=“Item1”; myArray[2]=“Item2”; for(i=0;i<myArray.length;i++){ document.write(myArray+“<br>”); } //--> </script> 设定将来执行 <script> <!-- functionhello(){ window.alert(“Hello”); } window.setTimeout(“hello()”,5000); //--> </script> 定时执行函数 <script> <!-- functionhello(){ window.alert(“Hello”); window.setTimeout(“hello()”,5000); } window.setTimeout(“hello()”,5000); //--> </script> 取消定时执行 <script> <!-- functionhello(){ window.alert(“Hello”); } varmyTimeout=window.setTimeout(“hello()”,5000); window.clearTimeout(myTimeout); //--> </script> 在页面卸载时候执行函数 <bodyonUnload=”functionName();”> Bodyofthepage </body>
本文出自 “博客即日起停止更新” 博客,请务必保留此出处http://sucre.blog.51cto.com/1084905/380691
相关文章推荐
- JavaScript使用过程中需要注意的地方和一些基本语法
- extjs4 对javascript基本语法扩展支持(转)
- JavaScript正则表达式上之基本语法(推荐)
- JavaScript高级程序设计——第3章 基本语法
- javascript基本语法2
- JavaScript 基本语法
- 说说 JSON 的基本语法以及与 JavaScript 的异同
- JavaScript使用过程中需要注意的地方和一些基本语法
- JavaScript基本语法(二)
- JavaScript标准教程之基本语法
- JavaScript学习记录02--基础篇之javascript基本语法(一)
- day15 CSS 定位 JavaScript 基本语法、基于对象编程、事件处理机制
- 读书笔记 JavaScript高级程序设计 第三章 基本概念-语法
- JavaScript 正则表达式上——基本语法
- javascript基本语法6 其他引用类型对象
- JavaScript学习-基本语法
- JavaScript---基本语法学习(一)JavaScript概述、Java和JavaScript的区别、和HTML结合方式、数据类型、运算符和表达式
- (1)javascript 基本概念 -- 语法
- JavaScript 基本语法
- 韩顺平_轻松搞定网页设计(html+css+javascript)_第19讲_js运行原理_js开发工具介绍_js程序(hello)_js基本语法_学习笔记_源代码图解_PPT文档整理