Java Script study
1.Java script包括3部分:
1]基础语法 2]DOM(document object model) 3]BOM(brower object model)
2.Title head link base script 这些标签可以出现在head标签间
3.在网页中加入javascript -------->使用<script>....</script>标记javascript的开始和结束
Script可以出现在html的任意地方,一般函数定义出现在head部分,真正的函数定义出现在body里面,一个一面可以有多个<script>...</script>,并且不同部份的方法和变量可以共享。
1]在head之间
<head>
<script type="text/javascript">
alert("hello javascript!");
alert("ok!");
</script>
</head>
/*****************************************************************/
<head>
<script language="javascript" src="2.js"> /引入一个外部的javascript文件/
</script>
</head>
2]在Body之间
<script language="javascript">
document.write("<br>这是以javascript打印出的!</br>");
</script>
4.javascript变量
规则:
1】建议先定义,在使用
2】区分大小写
3】弱类型的语言,用var定义
4】可不定义直接使用,但不建议这么做
<head>
<script>
var greeting;
greeting="welcom to javascript!";
</script>
</head>
<body>
<script language="javascript">
document.write(greeting);
</script>
</body>
5.javascript调试
工具-->internet选项-->高级(打开禁用脚本调试)(显示每个脚本错误的通知)
6.javascript数组
<head>
<script language="javascript">
var arr=new Array(3);
arr[0]=0;
arr[1]=1;
arr[2]=2;
arr[3]=3;
arr[4]=4;
</script>
</head>
<body>
<script language="javascript">
document.write(arr[4]);
</script>
</body>
Javascript基本语法-------函数
1.利用function定义一个函数 ag:javascript7.html
<head>
<script language="javascript">
function test(){
document.write("a");
document.write("b");
}
</script>
</head>
<body>
<script language="javascript">
test();
</script>
</body>
2.传入参数(函数带参数) eg:javascript8.html
<head>
<script language="javascript">
function test(a){
document.write(a);
}
</script>
</head>
<body>
<script language="javascript">
test();
</script>
</body>
3.传出值 eg:javascript9.html
<head>
<script language="javascript">
function test(a){
return a*3.14;
}
</script>
</head>
<body>
<script language="javascript">
document.write(test(12.3));
</script>
</body>
Javascript事件处理
1.onFocus(在用户为了输入而选择select/text/textarea等时会触发它) eg:javascript10.html
<body>
<form name="test">
<input type="test" name="userName" value="sxt" onFocus="javascript:alert(document.test.userName.value);">
</form>
</body>
2.onBlur(在select/text/password/textarea失去焦点时触发)
3.onChange(在select/text/textarea的值被改变且失去焦点时触发)
4.onClick(一个对象被鼠标点中时) eg:javascript11.html
<body>
<img src="gjl.jpg" onClick="alert('ok');"></img>
</body>
5.onLoad(将文档load到一个窗口时,一般写在body标签里)
Eg:javascript12.html
<body onLoad="javascript:alert('hello');" onUnload="javascript:alert('bye-bye');">
nihao
</body>
6.onUnload(当用户退出一个文档时) 如5
7.onMouseOver(鼠标移到某个对象时触发)
<body>
<img src="gjl.jpg" οnmοuseοver="javascript:alert('over');" οnmοuseοut="javascript:alert('out');"></img>
</body>
8.onMouseOut(鼠标从一个对象上移开时触发) 如7
9.onSelect(当form中的对象被选中时触发) eg:javascript13.html
<body onLoad="javascript:alert('hello');" onUnload="javascript:alert('bye-bye');">
nihao
<form name="test">
<input type="text" name="t" value="hello" onSelect="javascript:alert('');">
//输入框中的值被选中
<select name="se">
<option value="0">hehe</option>
<option value="1">hehe</option>
</select>
</form>
</body>
10.onSubmit(用户通过提交按钮提交一个表单时触发---是在真正提交之前)
*可以使用它进行表单验证eg:
<head>
<script language="javascript">
function check(){
if(document.test.t.value==""){//*****特别注意等于空时不能写成“ ”,只能“”
alert("空字符串不允许!");
return false;
}
return true;
}
</script>
</head>
<body>
<form name="test" action="javascript7.html" οnsubmit="return check()">
<input type="text" name="t">
<input type="submit" value="ok">
</form>
</body>
Eg:javascript15.html
<body>
<form name="test" action="javascript7.html" οnsubmit="alert('ok');">
<input type="submit" value="ok">
</form>
</body>
/********************return true表示继续提交*************************/
<body>
<form name="test" action="javascript7.html" οnsubmit="return true">
<input type="submit" value="ok">
</form>
</body>
/********************return false表示中断提交*********************/
<body>
<form name="test" action="javascript7.html" οnsubmit="return false">
<input type="submit" value="ok">
</form>
</body>
Javascript 对话框
1.警告框(alert):出现一个提示信息
<head>
<script language="javascript">
alert("red hot");
</script>
</head>
2.询问框(prompt):返回输入的值-------很少用
<head>
<script language="javascript">
var username=prompt("请输入你的名字:");
document.write("你好"+ username);
</script>
</head>
3.确认框(confirm):根据不同的选择,返回true/false------用于删除什么东西的时候
<head>
<script language="javascript">
function confirmit(){
if(confirm("你确定删除此文件吗?")){
document.tee.submit();
}
}
</script>
</head>
<body>
<form name="tee" action="tttt.html" method="post">
<input type="button" value="删除文件" name="aa" οnclick="javascript:confirmit()">
</form>
</body>
Javascript内置对象
1.This:指当前对象 eg:javascript20.html
<body>
<img src="gjl.jpg" οnclick="javascript:alert(this.src)"></img>//指当前标签,即是img
</body>
/**************************************************/
<head>
<script language="javascript">
function checkit(obj){
if(obj.value==""){
alert("空值");
}else
alert(obj.value);
}
</script>
</head>
<body>
<form name="tt">
<input type="test" name="aa" value="bb" οnclick="javascript:checkit(this)">
//指当前标签,即是input
</form>
</body>
2.For...in:in后跟一个对象,对此对象中的所有元素循环一次
<body>
<script language="javascript">
var a=new Array("a","b","c","d");
for(eee in a){
document.write(eee+"----");//eee是属性
document.write(a[eee]+"<br>");
}
</script>
</body>
3.With:为一段代码建立一个缺省的对象,任何无对象的引用,都将使用该缺省对象
<body>
<script language="javascript">
with(document){
write("11111");
write("11112");
write("11113");
write("11114");
}
</script>
</body>
4.New:用于生成一个新的对象
<body>
<script language="javascript">
var today=new Date();
alert(today.getDate());
</script>
</body>
窗口中的对象和元素
1.Window
1】当前窗口???5:10
<body>
<script language="javascript">
window.status="hello,please look here!";
</script>
<form name="tt">
<input type="button" value="test" onMouseOver="javascript:window.satatus='haha';">
</form>
</body>
2】新开窗口
<body>
<script language="javascript">
window.open("javascript7.html");
</script>
</body>
/************************************************/
<body>
<script language="javascript">
window.open("javascript7.html" ,"newWin","toolbar=no ,left=200,top=100,menubar=no,width=100,height=100,resizable=no");
</script>
</body>
3】通过本地窗口控制新开窗口
<body>
<script language="javascript">
Var a=window.open("javascript7.html" ,"newWin","toolbar=no ,left=200,
top=100,menubar=no,width=100,height=100,resizable=no");
</script>
<input type="button" value="关闭" οnclick="javascript:a.close();">
</body>
2.Location(获取或设置现有文档的url)
<body>
<script language="javascript">
alert(window.location);
alert(document.location);//此两种写法效果一样
</script>
</body>
/************************************************************/
<script language="javascript">
function go2(){
window.location="javascript7.html";
}
</script>
</head>
<body>
<input type="button" value="转向" οnclick="javascript:go2();">
</body>
Note:这样写是错误的
<script language="javascript">
<input type="button" value="转向" οnclick="javascript:go2();">
</script>
3.History(先前访问过的url的历史列表,常用方法:back(),go(number))
<head>
<script language="javascript">
function goBack(){
history.back();
}
</script>
</head>
<body>
<input type="button" value="返回" οnclick="javascript:goBack();">
</body>
4.Document(当前的文档对象)
1】Document.write()--->向客户端浏览器输出内容
2】document.formName---->得到表单名称
3】document.referrer
产生动画效果
setTimeout(“show()” ,delay);
Calender relative