1. DOM编程实例01
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
tr{
height: 70px;
}
td ,th{
width: 150px;
text-align: center;
}
</style>
<script>
function addNode(){
var tab=document.getElementById("tab");
var tr =document.createElement("tr");
var td =document.createElement("td");
td.innerHTML="<input type='text' size='10px' οnblur='seaveVal(this)'/>"
var td2 =document.createElement("td");
td2.innerHTML="<input type='text' size='10px' οnblur='seaveVal(this)'/>"
var td3 =document.createElement("td");
td3.innerHTML="<input type='button' value='添加'/> "+
"<input type='button' value='删除' οnclick='removeNode(this)'/>"
tab.appendChild(tr);
tr.appendChild(td);
tr.appendChild(td2);
tr.appendChild(td3);
}
function seaveVal(thi){
var td =thi.parentNode;
td.innerText=thi.value;
}
function removeNode(thi){
var tr= thi.parentNode.parentNode;
tr.remove();
}
</script>
</head>
<body>
<table border="1px" align="center" id="tab">
<tr>
<th>图书名称</th>
<th>图书价格</td>
<th>操作</th>
</tr>
<tr>
<td>javaSE</td>
<td>19</td>
<td>
<input type="button" name="" id="" value="添加" onclick="addNode()"/>
<input type="button" name="" id="" value="删除" onclick='removeNode(this)' />
</td>
</tr>
<tr>
<td>javaEE</td>
<td>39</td>
<td>
<input type="button" name="" id="" value="添加" onclick="addNode()"/>
<input type="button" name="" id="" value="删除" onclick='removeNode(this)'/>
</td>
</tr>
</table>
</body>
</html>
tr{
height: 70px;
}
td ,th{
width: 150px;
text-align: center;
}
</style>
<script>
function addNode(){
//获得table表格对象
var tab=document.getElementById("tab");
//创建tr节点对象
var tr =document.createElement("tr");
//创建td对象
var td =document.createElement("td");
td.innerHTML="<input type='text' size='10px' onblur='seaveVal(this)'/>"
var td2 =document.createElement("td");
td2.innerHTML="<input type='text' size='10px' onblur='seaveVal(this)'/>"
var td3 =document.createElement("td");
td3.innerHTML="<input type='button' value='添加'/> "+
"<input type='button' value='删除' onclick='removeNode(this)'/>"
tab.appendChild(tr);
tr.appendChild(td);
tr.appendChild(td2);
tr.appendChild(td3);
}
2. DOM编程实例02
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
body{
background-image: url(img/bjsky.jpg);
background-repeat: no-repeat;
background-size: 1600px 800px;
}
a{
font-size: 25px;
color: #ffffff;
}
.div1{
width: 300px;
height: 200px;
background-color: gray;
text-align: center;
}
input{
width: 40px;
height: 30px;
}
</style>
<script>
var i=-1;
function changebc(){
var arr=['mayun.jpg','bjsky.jpg','simple.jpg','sxt.jpg','zgc.jpg'];
if(i<arr.length-1){
i++;
}else {
i=0;
}
document.body.style.backgroundImage="url(img/"+arr[i]+")"
}
function chengove(){
var div =document.getElementById("div_1");
div.style.marginTop= Math.random()*500+ "px";
div.style.marginLeft=Math.random()*800+"px";
}
function changeclk(){
document.body.style.backgroundImage="url(img/1.jpg)";
document.getElementById("div_1").style.display="none";
}
</script>
</head>
<body>
<a href="javascript:changebc()">点击更换主题</a>
<div class="div1" id="div_1">
<h3>我是不是你的小可爱</h3>
<input type="button" value="是" onclick="changeclk()"/>
<input type="button" value="否" onmouseover="chengove()"/>
</div>
</body>
</html>
3. 表单元素操作
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
function checkName(){
var uname =document.getElementById("uname").value;
var span =document.getElementById("uname_span");
if(uname==null||uname==""){
span.innerText="×账号不能为空";
}else {
span.innerText="√账号合法";
}
}
</script>
</head>
<body>
<form action="" name="fom" id="fom" >
<p>
账号:<input type="text" name="uname" id="uname" onblur="checkName()"/><span id="uname_span"></span>
</p>
<p>
密码:<input type="password" name="pwd" value="123"/>
</p>
<p>
<input type="button" value="提交" onclick="checkName()" />
</p>
</form>
</body>
</html>
4. 表单验证
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
var ran;
function YZM(){
ran=Math.floor(Math.random()*9000+1000);
var span =document.getElementById("yzm_span");
span.innerText=ran;
}
function checkName(){
var reg=/^[\u4e00-\u9fa5]{3,5}$/;
return check("uname",reg);
}
function checkEmail(){
var reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
return check("email",reg);
}
function checkPwd(){
var reg=/^\d{3,6}$/;
return check("pwd",reg);
}
function check(id,reg){
var uname=document.getElementById(id);
var val=uname.value;
var alt=uname.alt;
var span =document.getElementById(id+"_span");
if(val==null||val==""){
span.innerText="×"+alt+"不能为空";
span.style.color="red";
return false;
}else if(reg.test(val)){
span.innerText="√"+alt+"合法";
span.style.color="green";
return true;
}else{
span.innerText="×"+alt+"不合法";
span.style.color="red";
return false;
}
}
function checkSex(){
var sex=document.getElementsByName("sex");
var span =document.getElementById("sex_span");
for(var i in sex){
if(sex[i].checked){
span.innerHTML="性别选择成功";
span.style.color="green";
return true;
}
}
span.innerHTML="请选择性别";
span.style.color="red";
return false;
}
function checkAdress(){
var sel=document.getElementById("sel").value;
var span =document.getElementById("sel_span");
if(sel==0){
span.style.color="red";
span.innerText="请选择籍贯"
return false;
}else {
span.style.color="green";
span.innerText="籍贯选择成功"
return true;
}
}
function checkYZM(){
var yzm=document.getElementById("yzm").value;
var span =document.getElementById("yzm2_span");
if(ran==yzm){
span.style.color="green";
span.innerText="验证码正确";
return true;
}else {
span.style.color="red";
span.innerText="验证码不正确";
return false;
}
}
function checkAgree(){
var check=document.getElementById("check");
var sub=document.getElementById("sub");
sub.disabled=!check.checked;
}
function zong(){
var flag=checkName()&&checkPwd()&&checkSex()&&checkYZM()&&checkAgree()&&checkEmail();
return flag;
}
</script>
</head>
<body onload="YZM()">
<center>
<h3>注册页面</h3>
<hr />
<form action="" method="get" onsubmit="return zong()">
<table>
<tr height="35px">
<td width="150px">用户名:</td>
<td width="400px">
<input type="text" name="uname" id="uname" value="" alt="用户名" onblur="checkName()"/>
<span id="uname_span">*用户名必须是3-5位的汉字</span>
</td>
</tr>
<tr height="35px">
<td>密码:</td>
<td>
<input type="password" name="pwd" id="pwd" value="" alt="密码" onblur="checkPwd()"/>
<span id="pwd_span"></span>
</td>
</tr>
<tr height="35px">
<td>手机号:</td>
<td>
<input type="text" name="phone" id="phone" value="" alt="手机号" />
<span id="phone_span"></span>
</td>
</tr>
<tr height="35px">
<td>邮箱:</td>
<td>
<input type="text" name="email" id="email" value="" alt="邮箱" onblur="checkEmail()" />
<span id="email_span"></span>
</td>
</tr>
<tr height="35px">
<td>性别:</td>
<td>
男:<input type="radio" name="sex" id="" value="1" onclick="checkSex()"/>
女:<input type="radio" name="sex" id="" value="0" onclick="checkSex()"/>
<span id="sex_span">
</span>
</td>
</tr>
<tr height="35px">
<td>爱好:</td>
<td>
<input type="checkbox" name="fav" id="" value="1" />唱歌
<input type="checkbox" name="fav" id="" value="2" />睡觉
<input type="checkbox" name="fav" id="" value="3" />LOL<br />
<input type="checkbox" name="fav" id="" value="4" />旅游
<input type="checkbox" name="fav" id="" value="5" />高尔夫
<input type="checkbox" name="fav" id="" value="6" />篮球
</td>
</tr>
<tr height="35px">
<td>籍贯:</td>
<td>
<select name="adress" id="sel" onchange="checkAdress()">
<option value="0">--请选择--</option>
<option value="1">河南</option>
<option value="2">湖南</option>
<option value="3">海南</option>
<option value="4">云南</option>
</select>
<span id="sel_span"></span>
</td>
</tr>
<tr height="35px">
<td>验证码</td>
<td>
<input type="number" name="" id="yzm" value="" onblur="checkYZM()"/>
<span id="yzm_span"></span>
<span id="yzm2_span"></span>
</td>
</tr>
<tr height="35px">
<td>个人介绍:</td>
<td>
<textarea name="intro" rows="8" cols="30"></textarea>
</td>
</tr>
<tr height="35px">
<td colspan="2" align="center">
<input type="checkbox" name="" id="check" value="" onclick="checkAgree()">是否同一本公司协议
</td>
</tr>
<tr height="35px">
<td colspan="2" align="center">
<input type="submit" id="sub" value="注册" disabled="true"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
5. 案例:制作购物车页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/reset.css"/>
<link rel="stylesheet" type="text/css" href="css/jd.css"/>
<link rel="stylesheet" type="text/css" href="icon/iconfont.css"/>
<script type="text/javascript" src="js/jd.js" ></script>
</head>
<body>
<div class="nav">
<div class="warp">
<ul class="nav_ul1">
<li><a href=""><i class="iconfont"></i> 京东首页</a></li>
<li><a href="">配送到:北京</a></li>
</ul>
<ul class="nav_ul2">
<li><a href="">洋洋宝贝</a><span>|</span></li>
<li><a href="">我的订单</a><span>|</span></li>
<li><a href="">我的京东</a><span>|</span></li>
<li><a href="">京东会员</a><span>|</span></li>
<li><a href="">企业采购</a><span>|</span></li>
<li><a href="">京东手机</a><span>|</span></li>
<li><a href="">关注京东</a><span>|</span></li>
<li><a href="">客户服务</a><span>|</span></li>
<li><a href="">网站导航</a></li>
</ul>
</div>
</div>
<div class="search">
<div class="warp">
<img src="img/logo.jpg" />
<div class="search_div">
<input type="text" class="search_text" />
<input type="button" value="搜索" class="search_but"/>
</div>
</div>
</div>
<div class="title warp">
<h3>全部商品</h3>
<div>
<span>配送到</span>
<select>
<option>昌平区</option>
<option>顺义区</option>
<option>大兴区</option>
<option>朝阳区</option>
<option>昌平区</option>
</select>
</div>
</div>
<div class="tips warp">
<ul>
<li>
<input type="checkbox" name="fav" id="all" onclick="checkTest1(this),checkTest2()"/>
全选
</li>
<li>商品</li>
<li>单价</li>
<li>数量</li>
<li>小计</li>
<li>操作</li>
</ul>
</div>
<div class="info warp">
<ul>
<li class="info_1"><input type="checkbox" name="fav" onclick="checkTest2()"/> </li>
<li class="info_2"> <img src="img/img1.jpg" width="80px"/> </li>
<li class="info_3"><a>【京东超市】desha春秋季儿童休闲服</a></li>
<li class="info_4"><a>颜色:灰色+粉红</a> </li>
<li class="info_5">182.5</li>
<li class="info_6">
<button onclick="checkTest3(this,1),checkTest2()">-</button>
<input type="text" name="" id="" value="1" />
<button class="bot" onclick="checkTest3(this,2),checkTest2()">+</button>
</li>
<li class="info_7">¥182.5</li>
<li>
<a href="javascript:viod(0)" onclick="checkTest4(this),checkTest2()">删除</a><br />
<a>已到我的关注</a>
</li>
</ul>
</div>
<div class="info warp">
<ul>
<li class="info_1"><input type="checkbox" name="fav" onclick="checkTest2()"/> </li>
<li class="info_2"> <img src="img/img1.jpg" width="80px"/> </li>
<li class="info_3"><a>【京东超市】desha春秋季儿童休闲服</a></li>
<li class="info_4"><a>颜色:灰色+粉红</a> </li>
<li class="info_5">182.5</li>
<li class="info_6">
<button onclick="checkTest3(this,1),checkTest2()">-</button>
<input type="text" name="" id="" value="1" />
<button class="bot" onclick="checkTest3(this,2),checkTest2()">+</button>
</li>
<li class="info_7">¥182.5</li>
<li>
<a href="javascript:viod(0)" onclick="checkTest4(this),checkTest2()">删除</a><br />
<a>已到我的关注</a>
</li>
</ul>
</div>
<div class="info warp">
<ul>
<li class="info_1"><input type="checkbox" name="fav" onclick="checkTest2()"/> </li>
<li class="info_2"> <img src="img/img1.jpg" width="80px"/> </li>
<li class="info_3"><a>【京东超市】desha春秋季儿童休闲服</a></li>
<li class="info_4"><a>颜色:灰色+粉红</a> </li>
<li class="info_5">182.5</li>
<li class="info_6">
<button onclick="checkTest3(this,1),checkTest2()">-</button>
<input type="text" name="" id="" value="1" />
<button class="bot" onclick="checkTest3(this,2),checkTest2()">+</button>
</li>
<li class="info_7">¥182.5</li>
<li>
<a href="javascript:viod(0)" onclick="checkTest4(this),checkTest2()">删除</a><br />
<a>已到我的关注</a>
</li>
</ul>
</div>
<div class="balance warp">
<ul class="balance_ul1">
<li>
<input type="checkbox" name="fav" id="" value="" onclick="checkTest1(this),checkTest2()"/>
全选
</li>
<li><a>删除选中商品</a></li>
<li><a>移到我的关注</a></li>
<li><a>清除下柜商品</a></li>
</ul>
<ul class="balance_ul2">
<li>已经选择<span id="snum">0</span>件商品</li>
<li>总价 <span id="zongz">¥0</span></li>
<li>
<button class="butt">去结算</button>
</li>
</ul>
</div>
</body>
</html>
6. 自定义对象prototype
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
function user(age,sex){
this.name="张三";
this.age=age;
this.sex=sex;
this.eat=function(){
alert("我是user中的吃的方法")
}
}
function user2(){
this.run=function(){
alert("我是user2中跑的方法");
}
}
user.prototype=new user2();
var us =new user(12);
us.look="看雾霾";
us.run();
</script>
</head>
<body>
</body>
</html>