JavaScript DOM 编程
(1).DOM概述及分类
(2).DOM结构模型:XML DOM 和 HTML DOM 关系?
(3).结点,结点树,结点属性与方法?
1.DOM是什么?
document object model 文档对象模型
是W3C织制订的一套用于访问XML和HTML文档的标准.允许脚本动态地访问和更新文档的内容、结构和样式.
分类
DOM Core
XML DOM
HTML DOM2.DOM结构模型:XML DOM 和 HTML DOM 关系?
3.
nodeName String 节点的名字;根据节点的类型而定义
nodeValue String 节点的值;根据节点的类型而定义
nodeType Number 节点的类型常量值之一
ownerDocument Document 指向这个节点所属的文档
firstChild Node 指向在childNodes列表中的第一个节点
lastChild Node 指向在childNodes列表中的最后一个节点
childNodes NodeList 所有子节点的列表
parentNode Node 返回一个给定节点的父节点。
previousSibling Node 指向前一个兄弟节点;如果这个节点就是第一个兄弟节点,那么该值为null
nextSibling Node 指向后一个兄弟节点;如果这个节点就是最后一个兄弟节点,那么该值为null
hasChildNodes() Boolean 当childNodes包含一个或多个节点时,返回真
attributes NamedNodeMap 包含了代表一个元素的特性的Attr对象;仅用于Element节点
appendChild(node) Node 将node添加到childNodes的末尾
removeChild(node) Node 从childNodes中删除node
replaceChild (newnode, oldnode) Node 将childNodes中的oldnode替换成newnode
insertBefore (newnode, refnode) Node 在childNodes中的refnode之前插入newnode
常用方法
getElementById()返回对拥有指定 id 的第一个对象的引用
getElementsByName()返回带有指定名称的对象集合
getElementsByTagName()返回带有指定标签名的对象集合DOM节点常见操作DOM 获取节点
DOM 改变节点
DOM 删除节点
DOM 替换节点
DOM 创建节点
DOM 添加节点模式对话框代码示例:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> js模式对话框 </title>
<script>
function openModal(){
//cval的值来自于window.returnValue返回的值,在打开的模式对话框中
var cval = window.showModalDialog("01modal.html"); //赋值
document.getElementById("city").value = cval;
}
</script>
</head> <body>
<input type="text" id="city" /><input type="button" value="请选择城市" onclick="openModal()"/>
</body>
</html><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> 模式对话框</title>
<script>
function getVal(obj){
window.returnValue = obj.value;
window.close();
}
</script>
</head> <body>
<input type="radio" value="北京" onclick="getVal(this)"/>北京<br/>
<input type="radio" value="深圳" onclick="getVal(this)"/>深圳<br/>
<input type="radio" value="东莞" onclick="getVal(this)"/>东莞<br/>
<input type="radio" value="武汉" onclick="getVal(this)"/>武汉<br/>
</body>
</html>键盘事件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> 提交事件 </title>
<!--
提交事件:onsubmit
1.是写在form表单中,而不是写在提交按钮中
2.调用格式:onsubmit="return 函数名()"
3.被调用的函数中一定要返回布尔值
-->
<script>
function chk(){
var username = document.getElementById("username").value;
var pwd = document.getElementById("pwd").value; //正则
var reg = /^[0-9a-zA-Z]{6,10}$/;
if(!reg.test(username)){
return false;
}
if(pwd==""){
return false;
}
return true;
}
</script>
</head> <body>
<form action="01modal.html" method="post" onsubmit="return chk()">
用户名:<input type="text" id="username" name="username" /><br/>
密码:<input type="password" id="pwd" name="pwd"/></br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>Onchange事件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>onchange事件 </title>
<script>
function changCity(){
//1.得到省份下拉列表的选中值
var provinceVal = document.getElementById("province").value;
//2.找到城市下拉列表
var cityObj = document.getElementById("city");
//3.定义数组
var citys = new Array();
citys[0] = new Array("");
citys[1] = new Array("济南","青岛");
citys[2] = new Array("长沙","株洲");
//4.添加城市下拉列表
//4.1清除下拉列表
cityObj.options.length=0;
var city = citys[provinceVal];
for(var i=0;i<city.length;i++){
cityObj.add(new Option(city[i],city[i]));// new Option(文本,值)
} }
</script>
</head> <body>
<select id="province" onchange="changCity()">
<option value="0">请选择省份</option>
<option value="1">山东</option>
<option value="2">湖南</option>
</select> <select id="city"></select>
</body>
</html>
获取DOM结点信息:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> DOM </title> </head> <body>
<h1 id="h1">明天休息</h1>
<script>
alert(document.getElementById("h1").innerHTML);
//alert(document.getElementById("h1").innerText);
//alert(document.getElementById("h1").firstChild.nodeValue);
//alert(document.getElementById("h1").lastChild.nodeValue);
//alert(document.getElementById("h1").childNodes[0].nodeValue);
//alert(document.getElementById("h1").textContent);//IE不好使,firefox好使
</script>
</body>
</html>
DOM 取值:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> 取li的文本值 </title>
<script>
function getLi(){
//1.找到bj的结点
var bjNode = document.getElementById("bj");
//2.找所有子结点
var bjChildNodessww = bjNode.childNodes;
//3遍历
for(var i=0;i<bjChildNodessww.length;i++){
var t = bjChildNodessww[i];
alert(t.nodeName+","+t.nodeValue+","+t.nodeType);
} }
</script>
</head> <body>
<ul>
<li id="bj" value="beijing">
北京
<h1>海淀</h1>
奥运
</li>
<li id="sh" value="shanghai">
上海
</li>
<br/>
<input type="button" value="li取值" onclick="getLi()"/>
</ul> </body>
</html>
遍历option文本:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> 得到所有的option的文本 </title> </head> <body>
<select name="edu" id="edu">
<option value="本科">本科^^</option>
<option value="专科">专科^^</option>
<option value="高中">高中^^</option>
<option value="小学">小学^^</option>
<option value="幼儿园">幼儿园^^</option>
</select>
<script>
/* //1.找到edu
var eduNode = document.getElementById("edu");
//2.得到孩子们
var eduChildNodesw = eduNode.childNodes;
//3.遍历孩子们
for(var i=0;i<eduChildNodesw.length;i++){
alert(eduChildNodesw[i].innerHTML);
}
*/
var eduNode = document.getElementsByTagName("option"); //3.遍历孩子们
for(var i=0;i<eduNode.length;i++){
alert(eduNode[i].innerHTML);
}
</script>
</body>
</html>
结点替换:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>结点替换</title>
<script>
function changeNode(){
//1.找到bj结点
var bjNodess = document.getElementById("bj");
//2.找到game结点,还要复制结点cloneNode(true);//true代表子结点也克隆
var gameNodess = document.getElementById("game").cloneNode(true);
//3.替换
bjNodess.replaceNode(gameNodess);
//bjNodess.parentNode.replaceChild(gameNodess,bjNodess);
}
</script>
</head> <body>
<ul >
<li id="bj" onclick="changeNode()">北京</li>
<li>湖南</li>
<li>山东</li>
</ul>
<ul>
<li id="game">打<p>灰机</p></li>
<li>抓泥鳅</li>
<li>斗地主</li>
</ul> </body>
</html>
添加结点:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<script>
function addOptions(){
//1.找到select标签
var ss = document.getElementsByTagName("select")[0];
//2.添加一个option结点
var optionss = document.createElement("option");
//3.添加一个文本结点,小学
//var xs = document.createTextNode("小学");
optionss.innerHTML="小学";
//4.搞关系
//optionss.appendChild(xs);
ss.appendChild(optionss); }
</script>
</head> <body>
<input type="button" value="添加选项" onclick="addOptions()"/>
<select>
<option>本科</option>
<option>专科</option>
<option>高中</option>
<option>初中</option>
</select> </body>
</html>表格操作
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> 表格操作 </title>
<script>
//--------------XML DOM--------------------------------------
/*function addTR(){
//1.取三个框的值
var username = document.getElementById("username").value;
var email = document.getElementById("email").value;
var age = document.getElementById("age").value;
//2.添加tr,td
var trObj = document.createElement("tr");
var tdUsername = document.createElement("td");
var tdEmail = document.createElement("td");
var tdAge = document.createElement("td");
var tdOp = document.createElement("td");
//3.添加td的内容
tdUsername.innerHTML=username;
tdEmail.innerHTML=email;
tdAge.innerHTML=age;
tdOp.innerHTML="<input type='button' value='删除' onclick='deleteRowss(this)'/>";
//4.搞关系
trObj.appendChild(tdUsername);
trObj.appendChild(tdEmail);
trObj.appendChild(tdAge);
trObj.appendChild(tdOp); //5.把tr添加到表格中
document.getElementById("tabInfo").appendChild(trObj); }
function deleteRowss(obj){
var trObjss = obj.parentNode.parentNode;//tr
trObjss.parentNode.removeChild(trObjss);
}*/ //---------------------HTML DOM---------------------------------
/*function addTR(){
//1.取三个框的值
var username = document.getElementById("username").value;
var email = document.getElementById("email").value;
var age = document.getElementById("age").value;
//2.添加tr,td
var tabinfoss = document.getElementById("tabInfo");
//在表格中添加一行
var trss = tabinfoss.insertRow();
var tdUsernamess = trss.insertCell();
var tdEmailss = trss.insertCell();
var tdAgess = trss.insertCell();
var tdOpss = trss.insertCell();
//3.添加td的内容
tdUsernamess.innerHTML=username;
tdEmailss.innerHTML=email;
tdAgess.innerHTML=age;
tdOpss.innerHTML="<input type='button' value='删除' onclick='deleteRowss(this)'/>"; }
function deleteRowss(obj){
//1.得到表格
var tabinfoss = document.getElementById("tabInfo"); //2.找到当前行的索引 rowIndex
var rowindexss = obj.parentNode.parentNode.rowIndex;
//3.删除
tabinfoss.deleteRow(rowindexss);//当前行的索引
}*/ //--------------------------------------------------------------
function addTR(){
//1.取三个框的值
var username = document.getElementById("username").value;
var email = document.getElementById("email").value;
var age = document.getElementById("age").value;
//2.添加tr,td
var tabinfoss = document.getElementById("tabInfo"); tabinfoss.innerHTML+="<tr><td>"+username+"</td><td>"+email+"</td><td>"+age+"</td><td><input type='button' value='删除' onclick='deleteRowss(this)'/></td></tr>";
} function deleteRowss(obj){
//1.得到表格
var tabinfoss = document.getElementById("tabInfo"); //2.找到当前行的索引 rowIndex
var rowindexss = obj.parentNode.parentNode.rowIndex;
//3.删除
tabinfoss.deleteRow(rowindexss);//当前行的索引
}
</script>
</head> <body>
<table align="center" cellpadding="10" cellspacing="3" >
<tr>
<td>姓名:<input type="text" id="username"/></td>
<td>Email:<input type="text" id="email"/></td>
<td>年龄:<input type="text" id="age"/></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="button" value="添加" onclick="addTR()"/></td>
</tr>
</table> <br/>
<br/>
<br/>
<hr/> <table id="tabInfo" align="center" width="500" border="1">
<tr>
<td>姓名</td>
<td>Email</td>
<td>年龄</td>
<td>操作</td>
</tr> </table> </body>
</html>
表格操作:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> 表格操作 </title>
<script>
//--------------XML DOM--------------------------------------
/*function addTR(){
//1.取三个框的值
var username = document.getElementById("username").value;
var email = document.getElementById("email").value;
var age = document.getElementById("age").value;
//2.添加tr,td
var trObj = document.createElement("tr");
var tdUsername = document.createElement("td");
var tdEmail = document.createElement("td");
var tdAge = document.createElement("td");
var tdOp = document.createElement("td");
//3.添加td的内容
tdUsername.innerHTML=username;
tdEmail.innerHTML=email;
tdAge.innerHTML=age;
tdOp.innerHTML="<input type='button' value='删除' onclick='deleteRowss(this)'/>";
//4.搞关系
trObj.appendChild(tdUsername);
trObj.appendChild(tdEmail);
trObj.appendChild(tdAge);
trObj.appendChild(tdOp); //5.把tr添加到表格中
document.getElementById("tabInfo").appendChild(trObj); }
function deleteRowss(obj){
var trObjss = obj.parentNode.parentNode;//tr
trObjss.parentNode.removeChild(trObjss);
}*/ //---------------------HTML DOM---------------------------------
/*function addTR(){
//1.取三个框的值
var username = document.getElementById("username").value;
var email = document.getElementById("email").value;
var age = document.getElementById("age").value;
//2.添加tr,td
var tabinfoss = document.getElementById("tabInfo");
//在表格中添加一行
var trss = tabinfoss.insertRow();
var tdUsernamess = trss.insertCell();
var tdEmailss = trss.insertCell();
var tdAgess = trss.insertCell();
var tdOpss = trss.insertCell();
//3.添加td的内容
tdUsernamess.innerHTML=username;
tdEmailss.innerHTML=email;
tdAgess.innerHTML=age;
tdOpss.innerHTML="<input type='button' value='删除' onclick='deleteRowss(this)'/>"; }
function deleteRowss(obj){
//1.得到表格
var tabinfoss = document.getElementById("tabInfo"); //2.找到当前行的索引 rowIndex
var rowindexss = obj.parentNode.parentNode.rowIndex;
//3.删除
tabinfoss.deleteRow(rowindexss);//当前行的索引
}*/ //--------------------------------------------------------------
function addTR(){
//1.取三个框的值
var username = document.getElementById("username").value;
var email = document.getElementById("email").value;
var age = document.getElementById("age").value;
//2.添加tr,td
var tabinfoss = document.getElementById("tabInfo"); tabinfoss.innerHTML+="<tr><td>"+username+"</td><td>"+email+"</td><td>"+age+"</td><td><input type='button' value='删除' onclick='deleteRowss(this)'/></td></tr>";
} function deleteRowss(obj){
//1.得到表格
var tabinfoss = document.getElementById("tabInfo"); //2.找到当前行的索引 rowIndex
var rowindexss = obj.parentNode.parentNode.rowIndex;
//3.删除
tabinfoss.deleteRow(rowindexss);//当前行的索引
}
</script>
</head> <body>
<table align="center" cellpadding="10" cellspacing="3" >
<tr>
<td>姓名:<input type="text" id="username"/></td>
<td>Email:<input type="text" id="email"/></td>
<td>年龄:<input type="text" id="age"/></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="button" value="添加" onclick="addTR()"/></td>
</tr>
</table> <br/>
<br/>
<br/>
<hr/> <table id="tabInfo" align="center" width="500" border="1">
<tr>
<td>姓名</td>
<td>Email</td>
<td>年龄</td>
<td>操作</td>
</tr> </table> </body>
</html>全选反选
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> 全选/反选/全不选 </title>
<script>
function checkAll(booleanVal){
//1.根据名称得到一组值
var hobbys = document.getElementsByName("hobby");
for(var i=0;i<hobbys.length;i++){
//hobbys[i].setAttribute("checked",booleanVal);
hobbys[i].checked=booleanVal;
}
}
function reversess(){
//1.根据名称得到一组值
var hobbys = document.getElementsByName("hobby");
for(var i=0;i<hobbys.length;i++){
hobbys[i].checked=!hobbys[i].checked;
}
}
</script>
</head> <body>
<h1>请选择你的爱好:</h1>
全选/全不选<input type="checkbox" name="hobbys" onclick="checkAll(this.checked)" /><br/>
<input type="checkbox" name="hobby" value="football"/>足球
<input type="checkbox" name="hobby" value="basketball" />篮球
<input type="checkbox" name="hobby" value="swim" />游泳
<input type="checkbox" name="hobby" value="singing" />唱歌<br/> <input type="button" value="全选" onclick="checkAll(true)" /> <input type="button" value="全不选" onclick="checkAll(false)" />
<input type="button" value="反选" onclick="reversess()"/> </body> </body>
</html>
左移 右移
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>下拉列表,选项左移右移 </title>
<script>
function removeLeft(){
//1.找左边的下拉列表
var leftSelectObj = document.getElementById("left");
//2.找左边下拉列表中选中的项
var indexss = leftSelectObj.selectedIndex;//找到选中项的索引
var selectedOptss = leftSelectObj.options[indexss];//找到了选中的option,<option>选项1</option>
//3.找右边的下拉列表
var rightSelectObj = document.getElementById("right");
//4.移动
//rightSelectObj.add(selectedOptss);//IE不支持,firefox支持
rightSelectObj.appendChild(selectedOptss);
} function removeLeftAll(){
//1.找左边的下拉列表
var leftSelectObj = document.getElementById("left");
var selectedOptAll = leftSelectObj.options;//左边所有选项 //2.找右边的下拉列表
var rightSelectObj = document.getElementById("right"); //3.一边遍历左边选项,一边添加右边的选项
var length = selectedOptAll.length;
/*for(var i=0;i<length;i++){
rightSelectObj.appendChild(selectedOptAll[i]);
}*/ /*for(var i=selectedOptAll.length-1;i>=0;i--){
rightSelectObj.appendChild(selectedOptAll[i]);
}*/
for(var i=0;i<length;i++){
rightSelectObj.appendChild(selectedOptAll[0]);
}
}
</script>
</head> <body>
<table align="center">
<tr>
<td>
<select size="10" id="left">
<option>选项1</option>
<option>选项2</option>
<option>选项3</option>
<option>选项4</option>
<option>选项5</option>
<option>选项6</option>
<option>选项7</option>
<option>选项8</option> </select>
</td>
<td>
<input type="button" value="--->" onclick="removeLeft()"/><br/>
<input type="button" value="===>" onclick="removeLeftAll()"/><br/>
<input type="button" value="<---" onclick=""/><br/>
<input type="button" value="<===" onclick=""/><br/>
</td>
<td>
<select size="10" id="right">
<option>选项9</option>
</select>
</td>
</tr>
</table> </body>
</html>
级联加载:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> 级联加载 </title>
<script>
var cityList = new Array();
cityList['北京市'] = ['朝阳区','东城区','西城区', '海淀区','宣武区','丰台区','怀柔','延庆','房山'];
cityList['上海市'] = ['宝山区','长宁区','丰贤区', '虹口区','黄浦区','青浦区','南汇区','徐汇区','卢湾区'];
cityList['广州省'] = ['广州市','惠州市','汕头市','珠海市','佛山市','中山市','东莞市'];
cityList['深圳市'] = ['福田区', '罗湖区', '盐田区', '宝安区', '龙岗区', '南山区', '深圳周边'];
cityList['重庆市'] = ['俞中区', '南岸区', '江北区', '沙坪坝区', '九龙坡区', '渝北区', '大渡口区', '北碚区'];
cityList['天津市'] = ['和平区', '河西区', '南开区', '河北区', '河东区', '红桥区', '塘古区', '开发区'];
cityList['江苏省'] = ['南京市','苏州市','无锡市'];
cityList['浙江省'] = ['杭州市','宁波市','温州市'];
cityList['四川省'] = ['四川省','成都市'];
cityList['海南省'] = ['海口市'];
cityList['福建省'] = ['福州市','厦门市','泉州市','漳州市'];
cityList['山东省'] = ['济南市','青岛市','烟台市'];
cityList['江西省'] = ['江西省','南昌市'];
cityList['广西省'] = ['柳州市','南宁市'];
cityList['安徽省'] = ['安徽省','合肥市'];
cityList['河北省'] = ['邯郸市','石家庄市'];
cityList['河南省'] = ['郑州市','洛阳市'];
cityList['湖北省'] = ['武汉市','宜昌市'];
cityList['湖南省'] = ['湖南省','长沙市'];
cityList['陕西省'] = ['陕西省','西安市'];
cityList['山西省'] = ['山西省','太原市'];
cityList['黑龙江省'] = ['黑龙江省','哈尔滨市'];
cityList['其他'] = ['其他']; function changeCity(){
//1.找到城市下拉列表
var cityObj = document.getElementById("city");
//2.找到省份选中的值
var provinceVal = document.getElementById("province").value;
//3.清空原有的城市列表
cityObj.length=1;
//3.遍历省份选中值的城市列表,一边遍历一边在城市列表中添加
for(var i in cityList[provinceVal]){
cityObj.add(new Option(cityList[provinceVal][i],cityList[provinceVal][i]));
} } function loadProvince(){
//1.找到省份
var provinceObj = document.getElementById("province");
provinceObj.length=1;
//加强for循环
for(var index in cityList){
provinceObj.add(new Option(index,index));
}
}
window.onload = loadProvince; </script>
</head> <body>
<select id="province" onchange="changeCity()">
<option>--请选择省份--</option>
</select> <select id="city">
<option>--请选择城市--</option>
</select> </body>
</html>