您的位置:首页 > Web前端 > JavaScript

javascript学习笔记

2015-06-09 13:54 579 查看
在html中声明,只需加入<script></script>即可,要写的代码放在标签内

alert("....");弹出对话框

函数定义方法

function 函数名(){

}

若在按钮中调用

<button type="button" onclick="函数名()"></button>

JavaScript

对大小写敏感

将元素变为指定内容

getElementById(“id”).InnerHTML=”…..”;

Document.write(“覆盖页面”)

外部链接js

<script src=””></script>

注释

可以用//和/* */

函数无法运行

检查脚本中的语句和分号是否正确,如果一句错误,下面的都不会再运行

字符串

1、可以用” ”,也可以用’ ’,两者是等价的

2、字符串相加

var a = ‘hello,’;

var b = ‘world’;

var c = a + b;//c is “hello,word”

3、字符串与数字相加

变为字符串

数组定义

var car = new Array();

var car= new Array(“a”,”b”,”c”);

对象创建

var fon = new Object();

fon.name=”…”;

fon.age=”…”;

函数

function 函数名(参数1,参数2….){

}

返回参数

function 函数名(参数1,参数2….){

return 参数名;

}

退出函数

return ;

变量

局部变量在函数调用完之后删除

全局变量在页面关闭后删除

控制语句

if….. else

switch() case:

for(;;){}

While(){} do{}while()

异常处理

try{}catch(error){}

error.message;

抛出异常

throw “提示语”

<html>

<body>

<input type="text" id="demo"/>

<button type="button" onclick="myfunction()">提交</button>

<h id="mess"></h>

</body>

<script>

function myfunction(){

try{

var x = document.getElementById("demo").value;

if(x =="") throw "值为空";

if(x > 10) throw "值太大";

if(x < 0) throw"值太小";

}catch(err){

document.getElementById("mess").innerHTML="错误:"+err;

}

}

</script>

</html>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: