window对象学习
<html
>
<head
>
<title
>window对象学习
</title
>
<meta charset
="UTF-8"/>
<!--
BOM浏览器对象模型:是规范浏览器对js语言的支持(js调用浏览器本身的功能
)。
BOM的具体实现是window对象
window对象使用学习:
1、window对象不用
new,直接进行使用即可,类似Math的使用方式,window关键字可以省略不写。
2、框体方法
alert
:警告框 提示一个警告信息,没有返回
confirm
:确认框 提示用户选择一项操作(确定
/取消)
点击确定 返回
true
点击取消 返回
false
prompt
:提示框, 提示用某个信息的录入或者说收集
点击确定,返回当前用书录入的数据,默认返回空字符串
点击取消
,返回
null
3、定时和间隔执行方法
setTimeout
:指定的时间后执行指定的函数
参数
1:函数对象
参数
2:时间,单位毫秒。
返回值:返回当前定时器的id
setInterval
:每间隔指定的时间执行指定的函数
参数
1:函数对象
参数
2:时间,单位毫秒。
返回值:返回当前间隔器的id
clearTimeout
:用来停止指定的定时器
参数:定时器的id
clearInterval
:用来停止指定的间隔器
参数:间隔器的id
-->
<!--声明js代码域
-->
<script type
="text/javascript">
function testAlert(){
var a
=window
.alert("我是警告框");
alert(a
);
}
function testConfirm(){
var flag
=window
.confirm("你确定要删除吗?");
alert(flag
);
}
function testPrompt(){
var str
=window
.prompt("请输入昵称:");
alert(str
);
}
var idi
;
var ids
function testSetTimeout(){
idi
=window
.setTimeout(function(){
alert("我是定时执行");
},3000);
}
function testSetInterval(){
ids
=window
.setInterval(function(){
alert("我是间隔执行");
},2000);
}
function testClearTimeout(){
window
.clearTimeout(idi
);
}
function testClearInterval(){
window
.clearInterval(ids
);
}
</script
>
</head
>
<body
>
<h3
>window对象学习
</h3
>
<hr
/>
<input type
="button" name
="" id
="" value
="测试警告框" onclick
="testAlert();" />
<input type
="button" name
="" id
="" value
="测试确认框" onclick
="testConfirm()" />
<input type
="button" name
="" id
="" value
="测试提示框" onclick
="testPrompt()"/>
<hr
/>
<input type
="button" name
="" id
="" value
="测试setTimeout--定时执行" onclick
="testSetTimeout()"/>
<input type
="button" name
="" id
="" value
="测试setInterval--间隔执行" onclick
="testSetInterval()"/>
<input type
="button" name
="" id
="" value
="测试clearTimeout--停止指定的定时器" onclick
="testClearTimeout()" />
<input type
="button" name
="" id
="" value
="测试clearInterval--停止指定的间隔器" onclick
="testClearInterval()" />
</body
>
</html
>
<html
>
<head
>
<title
>js的window对象学习
2</title
>
<meta charset
="UTF-8"/>
<!--
js的window对象学习
1、子窗口方法
window
.open('子页面的资源(相对路径)','打卡方式','配置');
示例:window
.open('son.html','newwindow','height=400, width=600, top=100px,left=320px, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=no, status=yes');
注意
:
关闭子页面的方法window
.close(),但是此方法只能关闭open方法打开的子页面。
2、子页面调用父页面的函数
window
.opener
.父页面的函数
js的window对象的常用属性
地址栏属性
:location
window
.location
.href
="新的资源路径(相对路径/URL)"
window
.location
.reload()重新加载页面资源
历史记录属性
window
.history
.forward() 页面资源前进,历史记录的前进。
window
.history
.back() 页面资源后退,历史记录后退
window
.history
.go(index
) 跳转到指定的历史记录资源
注意window
.history
.go(0)相当于刷新。
屏幕属性
window
.srceen
.width
;
window
.screen
.height
;
浏览器配置属性
主体面板属性(document
)
-->
<!--声明js代码域
-->
<script type
="text/javascript">
function testOpen(){
window
.open('son.html','newwindow','height=400, width=600, top=100px,left=320px, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=no, status=yes');
}
function testFather(){
alert("父页面");
}
function testLocation(){
window
.location
.href
="http://www.baidu.com";
}
function testLocation2(){
window
.location
.reload();
}
function testHistory(){
window
.history
.forward();
}
function testHistory2(){
window
.history
.go(0);
}
function testScreen(){
var x
=window
.screen
.width
;
var y
=window
.screen
.height
;
alert(x
+":"+y
)
}
function testNa(){
alert(window
.navigator
.userAgent
);
}
</script
>
</head
>
<body
>
<h3
>js的window对象学习
2</h3
>
<hr
/>
<input type
="button" name
="" id
="" value
="测试open" onclick
="testOpen()"/>
<hr
/>
<input type
="button" name
="" id
="" value
="测试地址栏属性--location--跳转资源" onclick
="testLocation()" />
<input type
="button" name
="" id
="" value
="测试地址栏属性--location--重新加载资源" onclick
="testLocation2()" />
<br
/><br
/>
<input type
="button" name
="" id
="" value
="测试历史记录属性--history-前进" onclick
="testHistory();"/>
<input type
="button" name
="" id
="" value
="测试历史记录属性--history-go" onclick
="testHistory2();"/>
<br
/><br
/>
<input type
="button" name
="" id
="" value
="测试屏幕属性--screen" onclick
="testScreen()" />
<input type
="button" name
="" id
="" value
="测试浏览器配置属性--navigator" onclick
="testNa()" />
</body
>
</html
>
js的document对象
<!DOCTYPE html
>
<html
>
<head
>
<meta charset
="UTF-8">
<title
>document对象学习
</title
>
<!--
document对象学习
:
1、document对象的概念
浏览器对外提供的支持js的用来操作
HTML文档的一个对象,此对象封存的
HTML文档的所有信息。
2、使用document
获取
HTML元素对象
直接获取方式:
通过id
通过name属性值
通过标签名
通过
class属性值
间接获取方式:
父子关系
子父关系
兄弟关系
-->
<!--声明js代码域
-->
<script type
="text/javascript">
function testGetEleById(){
var inp
=window
.document
.getElementById("uname");
alert(inp
);
}
function testGetEleByName(){
var favs
=document
.getElementsByName("fav");
alert(favs
);
}
function testGetEleByTagName(){
var inps
=document
.getElementsByTagName("input");
alert(inps
);
}
function testGetEleByClassName(){
var inps
=document
.getElementsByClassName("common");
alert(inps
.length
);
}
function testParent(){
var showdiv
=document
.getElementById("showdiv");
var childs
=showdiv
.childNodes
;
alert(childs
.length
);
}
function testChild(){
var inp
=document
.getElementById("inp");
var div
=inp
.parentNode
;
alert(div
);
}
function testBrother(){
var inp
=document
.getElementById("inp");
var preEle
= inp
.previousSibling
;
var nextEle
=inp
.nextSibling
;
alert(preEle
+":::"+nextEle
);
}
</script
>
<style type
="text/css">
.common
{}
#showdiv
{
border
: solid
2px orange
;
width
: 300px
;
height
: 300px
;
}
</style
>
</head
>
<body
>
<h3
>document对象的概念和获取元素对象学习
</h3
>
直接获取方式学习:
<br
/>
<input type
="button" name
="" id
="" value
="测试获取HTML元素对象--id" onclick
="testGetEleById()" />
<input type
="button" name
="" id
="" value
="测试获取HTML元素对象---name" onclick
="testGetEleByName()" />
<input type
="button" name
="" id
="" value
="测试获取HTML元素对象---TagName" onclick
="testGetEleByTagName()" />
<input type
="button" name
="" id
="" value
="测试获取HTML元素对象---className" onclick
="testGetEleByClassName()" />
<hr
/>
用户名:
<input type
="text" name
="uname" id
="uname" value
="" /><br
/><br
/>
<input type
="checkbox" name
="fav" id
="fav" value
="" class="common"/>唱歌
<input type
="checkbox" name
="fav" id
="fav" value
="" class="common"/>跳舞
<input type
="checkbox" name
="fav" id
="fav" value
="" />睡觉
<input type
="checkbox" name
="fav" id
="fav" value
="" />打游戏
<hr
/>
间接获取方式学习:
<br
/>
<input type
="button" name
="" id
="" value
="测试父子关系" onclick
="testParent()"/>
<input type
="button" name
="" id
="" value
="测试子父关系" onclick
="testChild()"/>
<input type
="button" name
="" id
="" value
="测试兄弟关系" onclick
="testBrother()"/>
<hr
/>
<div id
="showdiv"><input type
="" name
="" id
="" value
="" /><input type
="" name
="" id
="inp" value
="" />
<input type
="" name
="" id
="" value
="" />
<input type
="" name
="" id
="" value
="" />
<input type
="" name
="" id
="" value
="" />
<input type
="" name
="" id
="" value
="" />
</div
>
</body
>
</html
>
该段代码复制过去自己测试每个元素之间的关系,如下图:
js操作元素的属性
<html
>
<head
>
<title
>js操作
HTML的元素属性
</title
>
<meta charset
="UTF-8"/>
<!--
js操作
HTML元素属性学习:
获取元素对象
操作元素属性
获取:
元素对象名
.属性名
元素对象名
.getAttribute("属性名");
修改
元素对象名
.属性名
=属性值
元素对象名
.setAttribute("属性名","属性值");
注意:
尽量的不要去修改元素的id值和name属性值。
使用自定义方式获取固有属性内容,value的值获取的是默认值,不能够获取到实时的用户数据。
-->
<!--声明js代码域
-->
<script type
="text/javascript">
function testField(){
var inp
=document
.getElementById("uname");
alert(inp
.type
+":"+inp
.name
+":"+inp
.id
+":"+inp
.value
);
}
function testField2(){
var inp
=document
.getElementById("uname");
inp
.value
="哈哈";
inp
.type
="button";
}
function testOwnField(){
var inp
=document
.getElementById("uname");
alert(inp
.getAttribute("abc"));
}
function testOwnField2(){
var inp
=document
.getElementById("uname");
inp
.setAttribute("abc","呵呵");
}
function testOper(){
var inp
=document
.getElementById("uname");
alert(inp
.getAttribute("type"));
alert(inp
.getAttribute("value"));
}
</script
>
</head
>
<body
>
<h3
>js操作
HTML的元素属性
</h3
>
<input type
="button" name
="" id
="" value
="测试获取元素属性--固有" onclick
="testField()" />
<input type
="button" name
="" id
="" value
="测试修改元素属性--固有" onclick
="testField2()" />
<input type
="button" name
="" id
="" value
="测试获取元素属性--自定义" onclick
="testOwnField()" />
<input type
="button" name
="" id
="" value
="测试修改元素属性--自定义" onclick
="testOwnField2()" />
<input type
="button" name
="" id
="" value
="测试操作元素自定义操作固有属性" onclick
="testOper()" />
<hr
/>
用户名
: <input type
="text" name
="uname" id
="uname" value
="" abc
="嘿嘿"/>
</body
>
</html
>
js操作元素的内容
<html
>
<head
>
<title
>js操作元素内容学习
</title
>
<meta charset
="UTF-8"/>
<!--声明css
-->
<style type
="text/css">
#div01
{
width
: 200px
;
height
: 200px
;
border
: solid
1px orange
;
}
</style
>
<!--
操作元素内容学习:
获取元素对象
获取
元素对象名
.innerHTML
元素对象名
.innerHTML
修改
元素对象名
.innerHTML
="新的值"
元素对象名
.innerHTML
=元素对象名
.innerHTML
+"新的值"
元素对象名
.innerText
="新的值"
-->
<!--声明js代码域
-->
<script type
="text/javascript">
function getContext(){
var div
=document
.getElementById("div01");
alert(div
.innerHTML
);
alert(div
.innerText
);
}
function updateContext(){
var div
=document
.getElementById("div01");
div
.innerHTML
="<b>你先上,皇军给你殿后,八嘎</b>";
}
function updateContext2(){
var div
=document
.getElementById("div01");
div
.innerText
="<b>你先上,皇军给你殿后,八嘎</b>";
}
</script
>
</head
>
<body
>
<h3
>js操作元素内容学习
</h3
>
<input type
="button" name
="" id
="" value
="测试获取元素内容---innerHTML&innerText" onclick
="getContext()"/>
<input type
="button" name
="" id
="" value
="测试修改元素内容--innerHTML" onclick
="updateContext()"/>
<input type
="button" name
="" id
="" value
="测试修改元素内容--innerText" onclick
="updateContext2()"/>
<hr
/>
<div id
="div01">
<b
>皇军,前面有八路的干活。
</b
>
<b
>皇军,前面有八路的干活。
</b
>
</div
>
</body
>
</html
>
js操作元素的样式
<html
>
<head
>
<title
>js操作元素的样式
</title
>
<meta charset
="UTF-8"/>
<!--声明css
-->
<style type
="text/css">
#showdiv
{
width
: 200px
;
height
: 200px
;
border
: solid
1px
;
}
.common
{
width
: 200px
;
height
: 200px
;
border
: solid
1px
;
}
.common2
{
width
: 200px
;
height
: 200px
;
border
: solid
1px
;
background
-color
: aqua
;
}
</style
>
<!--
js操作元素样式:
获取元素对象
通过style属性
元素对象名
.style
.样式名
="样式值"
元素对象名
.style
.样式名
=""
注意
:
以上操作,操作的是
HTML的style属性声明中的样式。而不是其他css代码域中的样式。
通过className
元素对象名
.className
="新的值"
元素对象名
.className
=""
-->
<!--声明js代码域
-->
<script type
="text/javascript">
function testOperCss(){
var showdiv
=document
.getElementById("showdiv");
showdiv
.style
.backgroundColor
="#FFA500";
showdiv
.style
.border
="solid 2px red";
showdiv
.style
.border
="";
}
function testOperCss2(){
var div01
=document
.getElementById("div01");
alert(div01
.className
);
div01
.className
="common2";
div01
.className
="";
}
</script
>
</head
>
<body
>
<h3
>js操作元素的样式
</h3
>
<input type
="button" name
="" id
="" value
="测试操作元素样式--style" onclick
="testOperCss()" />
<input type
="button" name
="" id
="" value
="测试操作元素样式--className" onclick
="testOperCss2()" />
<hr
/>
<div id
="showdiv" style
="border: solid 2px blue;">
</div
>
<div id
="div01" class="common">
</div
>
</body
>
</html
>
js操作元素的文档结构
<html
>
<head
>
<title
>js操作元素的文档结构
</title
>
<meta charset
="UTF-8"/>
<!--
js操作
HTML文档结构
:
增加节点
删除节点
第一种方式:使用innerHTML
div
.innerHTML
=div
.innerHTML
+"内容"
div
.innerHTML
=""
父节点
.removeChild(子节点对象
)
-->
<!--声明js代码域
-->
<script type
="text/javascript">
function testAdd(){
var showdiv
=document
.getElementById("showdiv");
showdiv
.innerHTML
=showdiv
.innerHTML
+"<div><input type='file' value='' /><input type='button' value='删除' οnclick='delInp(this)'/></div>";
}
function delInp(btn
){
var showdiv
=document
.getElementById("showdiv");
var cdiv
=btn
.parentNode
;
showdiv
.removeChild(cdiv
);
}
</script
>
</head
>
<body
>
<h3
>js操作元素的文档结构
</h3
>
<input type
="button" name
="" id
="" value
="继续上传" onclick
="testAdd()"/>
<hr
/>
<div id
="showdiv">
</div
>
</body
>
</html
>
<!DOCTYPE html
>
<html
>
<head
>
<meta charset
="UTF-8">
<title
>js操作文档结构
2</title
>
<!--
js操作文档结构
2:
获取元素对象
var obj
=document
.createElement("标签名");
元素对象名
.appendChild(obj
);
-->
<!--声明js代码域
-->
<script type
="text/javascript">
function testOper2(){
var showdiv
=document
.getElementById("showdiv");
var inp
=document
.createElement("input");
inp
.type
="file";
var btn
=document
.createElement("input");
btn
.type
="button";
btn
.value
="删除";
btn
.onclick=function(){
showdiv
.removeChild(inp
);
showdiv
.removeChild(btn
);
showdiv
.removeChild(br
);
}
var br
=document
.createElement("br");
showdiv
.appendChild(inp
);
showdiv
.appendChild(btn
);
showdiv
.appendChild(br
);
}
</script
>
</head
>
<body
>
<h3
>js操作文档结构
2</h3
>
<input type
="button" name
="" id
="" value
="继续上传" onclick
="testOper2()"/>
<hr
/>
<div id
="showdiv">
</div
>
</body
>
</html
>
js操作form表单
js操作form表单
操作,数据不会提交。
-->
<script type
="text/javascript">
function testForm(){
var fm
=document
.getElementById("fm");
var frm
=document
.frm
;
fm
.reset();
fm
.action
="http://www.baidu.com/s";
}
</script
>
</head
>
<body
>
<h3
>js操作form表单
</h3
>
<input type
="button" name
="" id
="" value
="测试操作form" onclick
="testForm()" />
<hr
/>
<form action
="#" method
="get" id
="fm" name
="frm">
<b
>用户名:
</b
><input type
="text" name
="uname" id
="uname" value
="" readonly
="readonly"/><br
/><br
/>
密码:
<input type
="password" name
="pwd" id
="pwd" value
="" disabled
="disabled"/><br
/><br
/>
<input type
="submit" name
="" id
="" value
="登录" />
</form
>
</body
>
</html
>
document操作表单元素
<html
>
<head
>
<title
>操作表单元素
</title
>
<meta charset
="UTF-8"/>
<!--
js操作多选框、单选框
被选中状态下在js中checked属性值为
true,未选中状态为
false;
js操作下拉框:
被选择的option对象在js中selected属性值为
true,未选中为
false
-->
<!--声明js代码域
-->
<script type
="text/javascript">
function testCheckBox(){
var favs
=document
.getElementsByName("fav");
for(var i
=0;i
<favs
.length
;i
++){
if(favs
[i
].checked
){
alert(favs
[i
].value
+":"+favs
[i
].checked
);
}
}
}
function testCheckBox2(){
var favs
=document
.getElementsByName("fav");
for(var i
=0;i
<favs
.length
;i
++){
favs
[i
].checked
=true;
}
}
function testCheckBox3(){
var favs
=document
.getElementsByName("fav");
for(var i
=0;i
<favs
.length
;i
++){
favs
[i
].checked
=!favs
[i
].checked
;
}
}
function testSel(){
var sel
=document
.getElementById("address");
var os
=sel
.options
;
for(var i
=0;i
<os
.length
;i
++){
if(os
[i
].selected
){
alert(os
[i
].value
+":"+os
[i
].text
);
}
}
}
</script
>
</head
>
<body
>
<h3
>操作表单元素
</h3
>
<hr
/>
<b
>操作多选框
</b
><br
/><br
/>
<input type
="checkbox" name
="fav" id
="fav" value
="1" />远走高飞
<br
/>
<input type
="checkbox" name
="fav" id
="fav" value
="2" />当
<br
/>
<input type
="checkbox" name
="fav" id
="fav" value
="3" />李白
<br
/>
<input type
="checkbox" name
="fav" id
="fav" value
="4" />杜甫
<br
/>
<input type
="checkbox" name
="fav" id
="fav" value
="5" />see you again
<br
/>
<input type
="checkbox" name
="fav" id
="fav" value
="6" />fade
<br
/><br
/><br
/>
<input type
="button" name
="" id
="" value
="播放" onclick
="testCheckBox()"/>
<input type
="button" name
="" id
="" value
="全选" onclick
="testCheckBox2()"/>
<input type
="button" name
="" id
="" value
="反选" onclick
="testCheckBox3()"/>
<hr
/>
<select name
="" id
="address" onchange
="testSel()">
<option value
="0">--请选择
--</option
>
<option value
="1" >北京
</option
>
<option value
="2">上海
</option
>
<option value
="3">广州
</option
>
</select
>
</body
>
</html
>
event对象的学习
<html
>
<head
>
<title
>event对象学习
</title
>
<meta charset
="UTF-8"/>
<!--
event对象学习:
1、event对象获取鼠标坐标
2、event对象获取键盘值
-->
<style type
="text/css">
#showdiv
{
width
: 300px
;
height
: 300px
;
border
: solid
1px
;
}
</style
>
<script type
="text/javascript">
function getMouse(event
){
var eve
=event
|| window
.event
;
var x
=eve
.clientX
;
var y
=eve
.clientY
;
alert(x
+":"+y
);
}
function getKey(event
){
var eve
=event
|| window
.event
;
var code
=eve
.keyCode
;
alert(code
);
}
</script
>
</head
>
<body
>
<h3
>event对象学习
</h3
>
<hr
/>
<div id
="showdiv" onmousemove
="getMouse(event)">
</div
>
<br
/><br
/>
<input type
="text" name
="" id
="" value
="" onkeydown
="getKey(event)"/>
</body
>
</html
>
大概效果就是鼠标停留在方框中时,会弹出该鼠标停留的坐标。
js表单校验
<html
>
<head
>
<title
>js校验form表单
</title
>
<meta charset
="UTF-8"/>
<!--声明css代码域
-->
<style type
="text/css">
body
{
background
-image
: url(img
/b
.jpg
);
}
tr
{
height
: 40px
;
}
#showdiv
{
border
: solid
1px #
FF0000;
border
-radius
: 10px
;
width
: 500px
;
margin
: auto
;
margin
-top
: 40px
;
}
table
{
margin
: auto
;
color
: white
;
}
span
{
font
-size
:13px
;
}
#codeSpan
{
font
-size
:20px
;
}
</style
>
<!--声明js代码域
-->
<script type
="text/javascript">
function createCode(){
var code
=Math
.floor(Math
.random()*9000+1000);
var span
=document
.getElementById("codeSpan");
span
.innerHTML
=code
;
}
function checkUname(){
var uname
=document
.getElementById("uname").value
;
var reg
=/^[\u4e00-\u9fa5]{2,4}$/
var span
=document
.getElementById("unameSpan");
if(uname
=="" || uname
==null){
span
.innerHTML
="用户名不能为空";
span
.style
.color
="red";
return false;
}else if(reg
.test(uname
)){
span
.innerHTML
="用户名ok";
span
.style
.color
="green";
return true;
}else{
span
.innerHTML
="用户名不符合规则";
span
.style
.color
="red";
return false;
}
}
function checkPwd(){
var pwd
=document
.getElementById("pwd").value
;
var reg
=/^[a-z]\w{5,7}$/;
var span
=document
.getElementById("pwdSpan");
if(pwd
=="" ||pwd
==null){
span
.innerHTML
="*密码不能为空";
span
.style
.color
="red";
return false;
}else if(reg
.test(pwd
)){
span
.innerHTML
="*密码ok";
span
.style
.color
="green";
return true;
}else{
span
.innerHTML
="*密码格式不正确";
span
.style
.color
="red";
return false;
}
checkPwd2();
}
function checkPwd2(){
var pwd
=document
.getElementById("pwd").value
;
var pwd2
=document
.getElementById("pwd2").value
;
var span
=document
.getElementById("pwd2Span");
if(pwd2
==""||pwd2
==null){
span
.innerHTML
="确认密码不能为空";
span
.style
.color
="red";
return false;
}else if(pwd
==pwd2
){
span
.innerHTML
="确认密码ok";
span
.style
.color
="green";
return true;
}else{
span
.innerHTML
="两次密码不一致";
span
.style
.color
="red";
return false;
}
}
function checkPhone(){
return checkField("phone",/^1[3,4,5,7,8]\d{9}$/);
}
function checkMail(){
return checkField("mail",/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$/ )
}
function checkAddress(){
var sel
=document
.getElementById("address").value
;
var span
=document
.getElementById("addressSpan");
if(sel
!=0){
span
.innerHTML
="籍贯选择成功";
span
.style
.color
="green";
return true;
}else{
span
.innerHTML
="籍贯不能为请选择";
span
.style
.color
="red";
return false;
}
}
function checkFav(){
var favs
=document
.getElementsByName("fav");
var span
=document
.getElementById("favSpan");
for(var i
=0;i
<favs
.length
;i
++){
if(favs
[i
].checked
){
span
.innerHTML
="爱好选择成功";
span
.style
.color
="green";
return true;
}
}
span
.innerHTML
="爱好至少选则一项";
span
.style
.color
="red";
return false;
}
function checkAgree(){
document
.getElementById("sub").disabled
=!document
.getElementById("agree").checked
;
}
function checkSub(){
checkUname();
checkPwd();
checkPwd2();
checkPhone();
checkMail();
checkAddress();
checkFav();
return checkUname()&&checkPwd()&&checkPwd2()&&checkPhone()&&checkMail()&&checkAddress()&&checkFav();
}
function checkField(id
,reg
){
var inp
=document
.getElementById(id
);
var va
=inp
.value
;
var alt
=inp
.alt
;
var span
=document
.getElementById(id
+"Span")
if(va
=="" ||va
==null){
span
.innerHTML
=alt
+"不能为空";
span
.style
.color
="red";
return false;
}else if(reg
.test(va
)){
span
.innerHTML
=alt
+"ok";
span
.style
.color
="green";
return true;
}else{
span
.innerHTML
=alt
+"不符合规则";
span
.style
.color
="red";
return false;
}
}
</script
>
</head
>
<body onload
="createCode()">
<div id
="showdiv">
<form action
="#" method
="get" onsubmit
="return checkSub()">
<table
>
<tr
>
<td width
="80px">用户名
:</td
>
<td width
="200px">
<input type
="text" name
="uname" id
="uname" value
="" onblur
="checkUname()" alt
="用户名"/><span id
="unameSpan">*2-4位汉字
</span
>
</td
>
</tr
>
<tr
>
<td
>密码
:</td
>
<td
>
<input type
="password" name
="pwd" id
="pwd" value
="" onblur
="checkPwd()"/>
<span id
="pwdSpan"></span
>
</td
>
</tr
>
<tr
>
<td
>确认密码
:</td
>
<td
>
<input type
="password" name
="pwd2" id
="pwd2" value
="" onblur
="checkPwd2()"/>
<span id
="pwd2Span"></span
>
</td
>
</tr
>
<tr
>
<td
>手机号
:</td
>
<td
>
<input type
="text" name
="phone" id
="phone" value
="" alt
="手机号" onblur
="checkPhone()"/>
<span id
="phoneSpan"></span
>
</td
>
</tr
>
<tr
>
<td
>邮箱:
</td
>
<td
>
<input type
="text" name
="mail" id
="mail" value
="" alt
="邮箱" onblur
="checkMail()"/>
<span id
="mailSpan"></span
>
</tr
>
<tr
>
<td
>性别
</td
>
<td
>
男
<input type
="radio" name
="sex" id
="sex" value
="0" checked
="checked"/>
女
<input type
="radio" name
="sex" id
="sex" value
="1" />
</td
>
</tr
>
<tr
>
<td
>籍贯
:</td
>
<td
>
<select name
="address" id
="address" onchange
="checkAddress()">
<option value
="0">--请选择
--</option
>
<option value
="1">北京
</option
>
<option value
="2">上海
</option
>
<option value
="3">广州
</option
>
</select
>
<span id
="addressSpan"></span
>
</td
>
</tr
>
<tr
>
<td
>爱好
:</td
>
<td
>
<input type
="checkbox" name
="fav" id
="" value
="1" onclick
="checkFav()"/>LOL
<input type
="checkbox" name
="fav" id
="" value
="2" onclick
="checkFav()"/>睡觉
<input type
="checkbox" name
="fav" id
="" value
="3" onclick
="checkFav()"/>吃饭
<br
/>
<input type
="checkbox" name
="fav" id
="" value
="4" onclick
="checkFav()"/>打豆豆
<input type
="checkbox" name
="fav" id
="" value
="5" onclick
="checkFav()"/>看电影
<input type
="checkbox" name
="fav" id
="" value
="6" onclick
="checkFav()" />听歌
<span id
="favSpan"></span
>
</td
>
</tr
>
<tr
>
<td
>个人介绍
:</td
>
<td
>
<textarea name
="intro" rows
="4" cols
="40" id
="intro"></textarea
>
</td
>
</tr
>
<tr
>
<td
>验证码
:</td
>
<td
>
<input type
="text" name
="code" id
="code" value
="" style
="width: 100px;"/> 
; 
; 
; 
; 
;
<span id
="codeSpan" onclick
="createCode()" style
="background-image: url(img/code.jpg.gif);color: black;"></span
>
</td
>
</tr
>
<tr
>
<td colspan
="2" align
="center"><input type
="checkbox" name
="" id
="agree" value
="" onclick
="checkAgree()"/>是否同意本公司协议
</td
>
</tr
>
<tr
>
<td colspan
="2" align
="center"><input type
="submit" name
="" id
="sub" value
="立即注册" disabled
="disabled"/></td
>
</tr
>
</table
>
</form
>
</div
>
</body
>
</html
>
效果图:动态监听鼠标或键盘事件
js操作表格
<html
>
<head
>
<title
>操作表格
</title
>
<meta charset
="UTF-8"/>
<!--
js操作表格学习
:
1、删除行:
行对象
.rowIndex
表格对象
.deleteRow(要删除的行对象的角标
);
2、修改单元内容
单元格对象
.innerHTML
="新的内容";
行对象
.cells
-->
<!--声明css
-->
<style type
="text/css">
body
{
text
-align
: center
;
}
#ta
{
margin
: auto
;
}
#ta tr
{
height
: 35px
;
}
</style
>
<!--声明js代码域
-->
<script type
="text/javascript">
function delRow(btn
){
var ta
=document
.getElementById("ta");
var tr
=btn
.parentNode
.parentNode
;
ta
.deleteRow(tr
.rowIndex
);
}
function updateRow(btn
){
var tr
=btn
.parentNode
.parentNode
;
var cell
=tr
.cells
[3];
if(!isNaN(Number(cell
.innerHTML
))){
cell
.innerHTML
="<input type='text' value='"+cell
.innerHTML
+"' οnblur='updateRow2(this)'/>";
}
}
function updateRow2(inp
){
var cell
=inp
.parentNode
;
cell
.innerHTML
=inp
.value
;
}
function chooseDel(){
var ta
=document
.getElementById("ta");
var chks
=document
.getElementsByName("chk");
for(var i
=1;i
<chks
.length
;i
++){
if(chks
[i
].checked
){
ta
.deleteRow(i
);
i
--;
}
}
}
function addRow(){
var ta
=document
.getElementById("ta");
var tr
=ta
.insertRow(1);
var cell0
=tr
.insertCell(0);
cell0
.innerHTML
="<input type='checkbox' name='chk'/>";
var cell1
=tr
.insertCell(1);
cell1
.innerHTML
=document
.getElementById("uname").value
;
var cell2
=tr
.insertCell(2);
cell2
.innerHTML
="李思";
var cell3
=tr
.insertCell(3);
cell3
.innerHTML
="49.88";
var cell4
=tr
.insertCell(4);
cell4
.innerHTML
="5";
var cell5
=tr
.insertCell(5);
cell5
.style
.textAlign
="center";
cell5
.innerHTML
="<input type='button' value='修改数量' οnclick='updateRow(this)'/><input type='button' value='删除' οnclick='delRow(this)'/>";
}
function copyRow(){
var ta
=document
.getElementById("ta");
var chks
=document
.getElementsByName("chk")
for(var i
=0;i
<chks
.length
;i
++){
if(chks
[i
].checked
){
var tr
=ta
.insertRow(ta
.rows
.length
);
tr
.innerHTML
=ta
.rows
[i
].innerHTML
;
}
}
}
function chooseAll(){
var ck
=document
.getElementById("ck");
var chks
=document
.getElementsByName("chk");
if(ck
.checked
){
for(var i
=0;i
<chks
.length
;i
++){
chks
[i
].checked
=true;
}
}else{
for(var i
=0;i
<chks
.length
;i
++){
chks
[i
].checked
=false;
}
}
}
function operCss(){
var trs
=document
.getElementById("ta").rows
;
for(var i
=0;i
<trs
.length
;i
++){
if(i
%2==0){
trs
[i
].style
.backgroundColor
="red";
}else{
trs
[i
].style
.backgroundColor
="green";
}
}
}
</script
>
</head
>
<body
>
<h3 align
="center">操作表格学习
</h3
>
<input type
="button" name
="" id
="" value
="删除" onclick
="chooseDel()"/>
<input type
="button" name
="" id
="" value
="添加行" onclick
="addRow()"/>
<input type
="button" name
="" id
="" value
="复制行" onclick
="copyRow()"/>
<input type
="button" name
="" id
="" value
="隔行变色" onclick
="operCss()"/>
书名:
<input type
="text" name
="uname" id
="uname" value
="" />
<hr
/>
<table border
="1px" id
="ta">
<tr style
="text-align: center;font-weight: bold;">
<td width
="50px" align
="left"><input type
="checkbox" name
="chk" value
="0" id
="ck" onclick
="chooseAll()"/></td
>
<td width
="200px">书名
</td
>
<td width
="100px">作者
</td
>
<td width
="100px">价格
</td
>
<td width
="200px">购买数量
</td
>
<td width
="200px" >操作
</td
>
</tr
>
<tr id
="t1">
<td
><input type
="checkbox" name
="chk" id
="chk" value
="0" /></td
>
<td
>java从入门到放弃
</td
>
<td
>wollo
</td
>
<td
>43.50</td
>
<td
>3</td
>
<td align
="center">
<input type
="button" name
="" id
="" value
="修改数量" onclick
="updateRow(this)"/>
<input type
="button" name
="" id
="" value
="删除" onclick
="delRow(this)"/>
</td
>
</tr
>
<tr
>
<td
><input type
="checkbox" name
="chk" id
="chk" value
="1" /></td
>
<td
>javaScript入门
</td
>
<td
>高淇
</td
>
<td
>77.60</td
>
<td
>2</td
>
<td align
="center">
<input type
="button" name
="" id
="" value
="修改数量" onclick
="updateRow(this)"/>
<input type
="button" name
="" id
="" value
="删除" onclick
="delRow(this)"/>
</td
>
</tr
>
<tr
>
<td
><input type
="checkbox" name
="chk" id
="chk" value
="2" /></td
>
<td
>Spring入门
</td
>
<td
>卢俊杰
</td
>
<td
>78.88</td
>
<td
>3</td
>
<td align
="center">
<input type
="button" name
="" id
="" value
="修改数量" onclick
="updateRow(this)"/>
<input type
="button" name
="" id
="" value
="删除" onclick
="delRow(this)"/>
</td
>
</tr
>
</table
>
</body
>
</html
>
模拟淘宝
<html
>
<head
>
<title
>模拟淘宝网
</title
>
<meta charset
="UTF-8"/>
<!--声明js代码域
-->
<script type
="text/javascript">
function operInImg(img
,src
){
img
.style
.border
="solid 1px";
var big
=document
.getElementById("big");
big
.src
="img/"+src
;
}
function operOutImg(img
){
img
.style
.border
="";
}
</script
>
<!--声明css代码域
-->
<style type
="text/css">
#showdiv
{
width
: 370px
;
height
: 400px
;
border
: solid
1px
;
border
-radius
: 20px
;
}
#ta
{
margin
: auto
;
margin
-top
: 10px
;
}
</style
>
</head
>
<body
>
<div id
="showdiv">
<table width
="349px" id
="ta">
<tr height
="300px">
<td colspan
="5"><img src
="img/show1_big.jpg" id
="big"/></td
>
</tr
>
<tr height
="60px">
<td
><img src
="img/show1.jpg" onmouseover
="operInImg(this,'show1_big.jpg')" onmouseout
="operOutImg(this)"/></td
>
<td
><img src
="img/show2.jpg" onmouseover
="operInImg(this,'show2_big.jpg')" onmouseout
="operOutImg(this)"/></td
>
<td
><img src
="img/show3.jpg" onmouseover
="operInImg(this,'show3_big.jpg')" onmouseout
="operOutImg(this)"/></td
>
<td
><img src
="img/show4.jpg" onmouseover
="operInImg(this,'show4_big.jpg')" onmouseout
="operOutImg(this)"/></td
>
<td
><img src
="img/show5.jpg" onmouseover
="operInImg(this,'show5_big.jpg')" onmouseout
="operOutImg(this)"/></td
>
</tr
>
</table
>
</div
>
</body
>
</html
>
大概效果是鼠标停留在下面的某个小图,则方框里显示对应的大图!!