javascript
变量
var 字符串 string 数字 number 布尔 boolean 数组 Array 对象 Null Undefined
2.数组:
var arr=[1,2,3];定义时赋值
var arr[];//空数组
var arr=new Array();//定义一个空数组 可以通过索引访问赋值
3. 逻辑控制语句
①if条件语句
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
if(条件) { //JavaScript代码; } else { //JavaScript代码; }
②switch多分支语句
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
switch (表达式) { case 常量1 : JavaScript语句1; break; case 常量2 : JavaScript语句2; break; default : JavaScript语句3; }
③for、while循环语句
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
for(初始化; 条件; 增量) { JavaScript代码; } while(条件) { JavaScript代码; }
④for-in
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var fruit=[ "apple", "orange", "peach","banana"]; for(var i in fruit){ document.write(fruit[i]+"<br/>"); }
4. 循环中断
①break
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<script type="text/javascript"> var i=0; for(i=0;i<=5;i++){ if(i==3){ break; } document.write("这个数字是:"+i+"<br/>"); } </script>
②continue
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<script type="text/javascript"> var i=0; for(i=0;i<=5;i++){ if(i==3){ continue; } document.write("这个数字是:"+i+"<br/>"); } </script>
5.对象
js也是一种基于对象的语言
6.创建对象
使用new关键字调用构造器创建对象
function person(name,age) { this.name=name; this.age=age; this.info=function(){ document.write(this.name+this.age); } } var hhs=new person("hhs",21); hhs.info();
使用Object直接创建对象
var hhs=new Object(); hhs.name="HHS"; hhs.age=18; hhs.info=function() { document.write(this.name+this.age); } hhs.info();
使用JSON语法创建对象 var p={name:"HHS" , age:18};
var person={ "name":"hhs", "age":21, "son":[//son属性为一个JSON数组,其中存储两个JSON对象 { "name":"qq", "age":"2" }, { "name":"ww", "age":"6" } ], "info":function(){ document.write(this.name+this.age); for(var child in this.son) { document.write(this.son[child].name+this.son[child].age);//获取儿子的信息 } } } person.info() --------------------- 作者:QuietHRH 来源:CSDN 原文:https://blog.csdn.net/quiethrh/article/details/81104226 版权声明:本文为博主原创文章,转载请附上博文链接!var person={ "name":"hhs", "age":21, "son":[//son属性为一个JSON数组,其中存储两个JSON对象 { "name":"qq", "age":"2" }, { "name":"ww", "age":"6" } ], "info":function(){ document.write(this.name+this.age); for(var child in this.son) { document.write(this.son[child].name+this.son[child].age);//获取儿子的信息 } } } person.info()
7.BOM
弹出新窗口URL
function myOpen() { //URL 重复标记 打开的窗口属性 window.open("http://www.baidu.com","baidu","left=20,top=50,location=no,toolbar=no"); }
对话框
function myConfirm() { var an=window.confirm("是否退出?"); if(an) { alert("bye"); } else { alert(":-)"); } } function myPrompt() { var an=window.prompt("请输入密码:","默认值");//确定返回文本框的值,取消返回null }
定时器打印时间
function time() { var d=new Date(); var year= d.getFullYear(); var month= d.getMonth()+1; var day= d.getDate(); var week= d.getDay(); var hours= d.getHours(); var min= d.getMinutes(); var sec= d.getSeconds(); switch (week) { case 0: week="星期日" break; case 1: week="星期一" break; case 2: week="星期二" break; case 3: week="星期三" break; case 4: week="星期四" break; case 5: week="星期五" break; case 6: week="星期六" break; default : week="无效" break; } /* 当条件判断成功时,将定时器关闭 if() { window.clearInterval(t); }*/ document.getElementById("time").innerHTML=year+"-"+month+"-"+day+"-"+" "+hours+":"+min+":"+sec; } var t= window.setInterval("time()",1000);