javaScript是一种基于对象的事件驱动型的解释型语言。
一、javascript关键字
break case catch coutinue default delete do else false finally for function
if in instanceof new null return switch this throw true try typeof
var void while with;
分类:
1)判断类:if else ; switch case default; break coutinue;
2)循环类:while do for in;
3)异常类:try catch finally throw;
4)逻辑类:true false;
5)类型类:typeof instanceof;
6)定义类:var null void function new
7)其他: return;
【注】null:在程序中表示没有值或者不是一个对象;
undefined:代表变量的值尚未指定或者对象属性根本不存在;
typeof: 查询数值当前类型。
typeof("test"+3)="string"; typeof(null)="object"; typeof(true+1)="number";
typeof(true+false)="number";
二、对象
1)javascript包含四种对象:(1)内置对象;(2)自定义对象;(3) 浏览器对象window; (4) ActiveX对象。
[js中除了null和undefined以外其他的数据类型都被定义为对象;]
2)11种内置对象:
Number String Boolean Array Date Math Object RegExp Function Error Global;
(1)数据对象:Number String Boolean;
(2)组合对象:Array Date Math;
(3)高级对象:Object RegExp Function Error Global;
3)ECMA V3标准定义的全局变量和全局函数名称:(注意大小写不能变,红色为11种内置对象)
arguments encodeURL Infinity Object String Array Error isFinite parseFloat
parseInt syntaxError Boolean escape isNaN TypeError Date eval Math
RangeError undefined decodeURL EvalError NaN ReferenceError unescape
decodeURLComponent Function Number RegExp URLError Global
分类:
1)、encodeURL 和 decodeURL; Infinity 和 isFinite;
parseFloat、parseInt 和 Number; escape 和 unescape;
Object、String、Array、Date、Boolean、Math、Error;
Error、syntaxError、RangeError、EvalError、ReferenceError URLError;
【附:】一些用法
1)escape和unescape:[unicode加密解密]
<html>
<head>
<title> new document </title>
<script type="text/javascript">
function convert(id,tag){
var ovalue=document.getelementbyid(id);
if(tag==1){
ovalue.innerhtml=escape(ovalue.value);
}
if(tag==2){
ovalue.innerhtml=unescape(ovalue.value);
}
}
</script>
</head>
<body>
<textarea rows="10" cols="45" id="cont">msg:[中华人民共和国成立60周年]</textarea><br/>
<input type="button" value="escape" οnclick="convert('cont',1)"/>
<input type="button" value="escape" οnclick="convert('cont',2)"/>
</body>
</html>
2)parseFloat、parseInt和Number
var temp=window.prompt("input the data:","12.35asdefg");
alert(parseFloat(temp)); //12.35;
alert(parseInt(temp)); //12;
alert(Number(temp)); // NaN; ----------- 'not a number'
alert(12/0); //Infinity; --------无穷大
alert(Number.MAX_VALUE); //
alert(Number.MIN_VALUE); //
3) eval函数:(可以把一个字符串当成一个表达式来执行);
返回值:通过计算 string 得到的值(如果有的话)。
说明:该方法只接受原始字符串作为参数,如果 string 参数不是原始字符串,那么该方法将不作任何改变地返回。因此请不要为 eval() 函数传递 String 对象来作为参数。
(1) alert("2+3"); //2+3
alert(eval("2+3")); // 5
(2) function swapTwo() //搜集的实例
{
var the_image = prompt("change parrot or cheese","");
window.document.the_image.src = "ant.gif";
}
不幸的是, JavaScript将会寻找名字叫 the_image而不是你所希望的"cheese"或者"parrot"的图象,
于是你得到了错误信息:”没听说过一个名为the_image的对象”。
还好,eval能够帮你得到你想要的对象。
function simpleSwap()
{
var the_image = prompt("change parrot or cheese","");
var the_image_name = "window.document." + the_image;
var the_image_object = eval(the_image_name);
the_image_object.src = "ant.gif";
}
4) isFinite() 检查某个值是否为有穷大的数。
isNaN() 检查某个值是否是数字。
三、算术运算
1)+、 — 、 *、 /、 %。
数字+字符串--->字符串; 数字+布尔值(false 0,true 1)---->数字; 字符串+布尔值--->字符串;
2)自增自减:++、-- ;
3)符合运算符:+=、-=、*=、/=、%= ;
四、内置对象的用法:
1、window:
1)、window.alert("提示信息内容!");
2)、window.prompt("请输入数字:","10");
3)、window.open("http://www.hao123.com","_self","chennal");
2、document:
1)、document.write("页面输出"); document.writeln("页面输出多了换行");
3、String的方法:
1)length; (汉字、字母长度都是1) 2)
1)格式编排方法:anchor(); blink(); fixed(); bold(); italics();strike(); big(); small(); sub(); sup();
fontcolor("red"); fontsize(7).
2)大小写转换:toLowerCase(); toUpperCase();
3)获取指定字符:charAt(index); charCodeAt(index);//返回指定位置index的unicode编码;
4)查询字符串:(索引都是从0开始的)
indexOf(findstr,index); lastindexOf(findstr);
match(regexp);返回匹配的字符串的数组,没匹配则为null。 regexp为正则表达式或字符串;
search(regexp);返回找到字符串的首字符索引。(同indexOf方法一样)
5)子字符串处理:substr(start,length);
substring(start,end);
slice(start,end); //(同substring(start,end))
替换子字符串:replace(findstr,tostr);
五、细节
1、注释: 有单行注释和多行注释。 // 和 /* …… */
那些不支持 JavaScript 的浏览器会把脚本作为页面的内容来显示。为了防止这种情况发生,我们可以使用这样的 HTML 注释标签:
<script type="text/javascript">
<!--
……
//-->
</script>
六、事件
事件通常与函数配合使用,这样就可以通过发生的事件来驱动函数执行。
HTML 4.0 的新特性之一是有能力使 HTML 事件触发浏览器中的动作(action),比如当用户点击某个 HTML 元素时启动一段 JavaScript。下面是一个属性列表,这些属性可插入 HTML 标签来定义事件动作。
FF: Firefox, N: Netscape, IE: Internet Explorer
属性 | 当以下情况发生时,出现此事件 | FF | N | IE |
---|---|---|---|---|
onabort | 图像加载被中断 | 1 | 3 | 4 |
onblur | 元素失去焦点 | 1 | 2 | 3 |
onchange | 用户改变域的内容 | 1 | 2 | 3 |
onclick | 鼠标点击某个对象 | 1 | 2 | 3 |
ondblclick | 鼠标双击某个对象 | 1 | 4 | 4 |
onerror | 当加载文档或图像时发生某个错误 | 1 | 3 | 4 |
onfocus | 元素获得焦点 | 1 | 2 | 3 |
onkeydown | 某个键盘的键被按下 | 1 | 4 | 3 |
onkeypress | 某个键盘的键被按下或按住 | 1 | 4 | 3 |
onkeyup | 某个键盘的键被松开 | 1 | 4 | 3 |
onload | 某个页面或图像被完成加载 | 1 | 2 | 3 |
onmousedown | 某个鼠标按键被按下 | 1 | 4 | 4 |
onmousemove | 鼠标被移动 | 1 | 6 | 3 |
onmouseout | 鼠标从某元素移开 | 1 | 4 | 4 |
onmouseover | 鼠标被移到某元素之上 | 1 | 2 | 3 |
onmouseup | 某个鼠标按键被松开 | 1 | 4 | 4 |
onreset | 重置按钮被点击 | 1 | 3 | 4 |
onresize | 窗口或框架被调整尺寸 | 1 | 4 | 4 |
onselect | 文本被选定 | 1 | 2 | 3 |
onsubmit | 提交按钮被点击 | 1 | 2 | 3 |
onunload | 用户退出页面 | 1 | 2 | 3 |