#JavaScript操作DOM对象
课件
1操作DOM
2节点和节点关系
3访问节点
使用getElement系列方法访问指定节点
getElementById()、getElementsByName()、getElementsByTagName()
根据层次关系访问节点
节点信息
4操作节点
操作节点的属性
创建和插入节点
删除和替换节点
5操作节点样式
Style
className
获取元素的样式
6HTML中元素属性
7元素属性应用
总结
代码
示例1:文档结构
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>DOM节点</title>
</head>
<body>
<img src="images/fruit.jpg" alt="水果" id="fruit">
<h1>喜欢的水果</h1>
<p>DOM应用</p>
</body>
</html>
示例2:访问节点lastChild.firstChild.nextSibling
consloe.log()控制台输出
body,ul,li,div,section,header{margin: 0;padding: 0;}
ul,li{list-style: none;}
body{
font-family: "微软雅黑";
font-size: 12px;
line-height: 28px;
}
#news{
border: 1px solid #cccccc;
width: 280px;
overflow: hidden;
margin: 5px auto 0 auto;
}
#news header{
border-bottom: 1px solid #cccccc;
font-size: 16px;
line-height: 40px;
padding-left:15px;
color: #666666;
font-weight: bold;
}
#news header a{text-decoration: none; color: #666666; float: right; padding-right: 10px; font-size: 12px;}
#news ul li{padding-left: 10px;}
#news ul li a{color: #686868; text-decoration: none; display: inline-block;}
#news ul li a:hover{color: #b02f4a;}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>访问节点</title>
<link rel="stylesheet" href="css/newStyle.css">
</head>
<body>
<section id="news"><header>京东快报<a href="#">更多 > </a></header><ul><li><a href="#">志玲姐姐:墨镜300减30</a></li><li><a href="#">京东无锡馆正式启动</a></li><li><a href="#">99元抢平板!品牌配件199-100</a></li><li><a href="#">节能领跑京东先行</a></li><li><a href="#">高洁丝实力撩货,领券五折</a></li></ul></section>
<script>
var obj=document.getElementById("news");
var str=obj.lastChild.firstChild.nextSibling.nextSibling.innerHTML;
var str=obj.lastChild.lastChild.previousSibling.previousSibling.innerHTML;
//var str=obj.lastElementChild.firstElementChild.innerHTML||obj.lastChild.firstChild.innerHTML;
console.log(str);
</script>
</body>
</html>
示例3:节点信息.innerHTML
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>节点信息</title>
</head>
<body>
<ul id="nodeList"><li>nodeName</li><li>nodeValue</li><li>nodeType</li></ul><p></p>
<script>
var nodes=document.getElementById("nodeList");
var type1=nodes.firstChild.nodeType;
var type2=nodes.firstChild.firstChild.nodeType;
var name1=nodes.firstChild.firstChild.nodeName;
var str=nodes.firstChild.firstChild.nodeValue;
var con="type1:"+type1+"<br/>type2:"+type2+"<br/>name1:"+name1+"<br/>str:"+str;
document.getElementById("nodeList").nextSibling.innerHTML=con;
</script>
</body>
</html>
示例4:操作节点属性setAttribute
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>选择你喜欢的书</title>
<style>
*{font-size: 12px; font-family: "Arial", "微软雅黑"; line-height: 25px;}
div{padding: 5px; text-align: center;
}
div span{display: block;}
</style>
</head>
<body>
<p>选择你喜欢的书:<input type="radio" name="book" onclick="book()">我和狗狗一起活下来 <input type="radio" name="book" onclick="book()">灰霾来了怎么办</p>
<div><img src="" alt="" id="image" onclick="img()"><span></span></div>
<script>
function book(){
var ele=document.getElementsByName("book");
var img=document.getElementById("image");
if(ele[0].checked){
img.setAttribute("src","images/dog.jpg");
img.setAttribute("alt","我和狗狗一起活下来");
img.nextSibling.innerHTML="我和狗狗一起活下来";
}
else if(ele[1].checked){
img.setAttribute("src","images/mai.jpg");
img.setAttribute("alt","灰霾来了怎么办");
img.nextSibling.innerHTML="灰霾来了怎么办";
}
}
function img(){
var alt=document.getElementById("image").getAttribute("alt");
alert("图片的alt:"+alt)
}
</script>
</body>
</html>
示例5:操作节点.appendChild
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>选择你喜欢的书</title>
<style>
*{font-size: 12px; font-family: "Arial", "微软雅黑"; line-height: 25px;}
div{padding: 5px; text-align: center;
}
div span{display: block;}
</style>
</head>
<body>
<p>选择你喜欢的书:<input type="radio" name="book" onclick="book()">我和狗狗一起活下来 <input type="radio" name="book" onclick="book()">灰霾来了怎么办</p>
<div></div>
<script>
function book(){
var ele=document.getElementsByName("book");
var bName=document.getElementsByTagName("div")[0];
if(ele[0].checked){
var img=document.createElement("img");
img.setAttribute("src","images/dog.jpg");
img.setAttribute("alt","我和狗狗一起活下来");
img.setAttribute("onclick","copyNode()")
bName.appendChild(img);
}
else if(ele[1].checked){
var img=document.createElement("img");
img.setAttribute("src","images/mai.jpg");
img.setAttribute("alt","灰霾来了怎么办");
img.setAttribute("onclick","copyNode()")
bName.appendChild(img);
}
}
function copyNode(){
var bName=document.getElementsByTagName("div")[0];
var copy=bName.lastChild.cloneNode(false);
bName.insertBefore(copy,bName.firstChild);
}
</script>
</body>
</html>
示例6:删除和替换节点removeChild、replaceChild
自定义del、rep
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>删除和替换节点</title>
<style>
*{padding: 0; margin: 0; font-size: 14px;}
ul,li{list-style: none;}
li{float: left; text-align: center; width: 140px;}
</style>
</head>
<body>
<ul>
<li>
<img src="images/f01.jpg" id="first">
<p><input type="button" value="删除我吧" onclick="del()"></p>
</li>
<li>
<img src="images/f02.jpg" id="second">
<p><input type="button" value="换换我吧" onclick="rep()"></p>
</li>
</ul>
<script>
function del(){
var delNode=document.getElementById("first");
delNode.parentNode.removeChild(delNode);
}
function rep(){
var oldNode=document.getElementById("second");
var newNode=document.createElement("img");
newNode.setAttribute("src","images/f03.jpg");
oldNode.parentNode.replaceChild(newNode,oldNode);
}
</script>
</body>
</html>
示例7:我的购物车.style
*{margin: 0;padding: 0; font-family: "Arial", "微软雅黑"; font-size: 12px; line-height: 25px;}
ul,li{list-style: none;}
#shopping{margin: 20px auto 0 auto; width: 320px;
}
#cart{
background: #f9f9f9 url("../images/cart.png") 20px 6px no-repeat;
border: solid 1px #dcdcdc;
float: right;
width: 100px;
height: 28px;
padding-left: 45px;
line-height: 28px;
position: relative;
cursor: pointer;
}
#cart span{
position: absolute;
color: #fff;
background: #dc1742;
display: block;
width: 15px;
height: 15px;
border-radius: 50%;
top:-5px;
right: 20px;
font-size: 8px;
line-height: 15px;
text-align: center;
}
#cartList{width: 310px; float: right; border: solid 1px #dcdcdc; display: none;}
#cartList h2{border-bottom: 1px dashed #cccccc; font-size: 14px; padding-left: 10px;}
#cartList li{float: left;}
#cartList li:nth-of-type(1){width: 65px; text-align: center;}
#cartList li:nth-of-type(2){width: 155px;}
#cartList li:nth-of-type(3){text-align: center; width: 85px;}
#cartList .footer{clear: both; height: 35px; line-height: 35px; background: #f5f5f5; padding:0 5px;}
#cartList .footer span{padding: 0 12px;}
#cartList .footer span:nth-of-type(2){
color: #fff;
background: #dc1742;
display: block;
height: 25px;
border-radius: 6px;
float: right;
text-align: center;
font-weight: bold;
margin-top: 5px;
}
#shopping .cartOver{
background-color: #ffffff;
z-index: 100;
border-bottom: none;
}
#shopping .cartListOver{
display:block;
position:relative;
top:-1px;
}
#shopping .cartOut{
background-color:#f9f9f9;
border-bottom:solid 1px #dcdcdc;
}
#shopping .cartListOut{
display:none;
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>我的购物车</title>
<link rel="stylesheet" href="css/shopping.css">
</head>
<body>
<section id="shopping">
<div id="cart" onmouseover="over()" onmouseout="out()">我的购物车<span>1</span></div>
<div id="cartList">
<h2>最新加入的商品</h2>
<ul>
<li><img src="images/makeup.jpg"></li>
<li>倩碧经典三部曲套装(液体皂200ml+明肌2号水200ml+润肤乳125ml)</li>
<li>¥558.00×1<br/>删除</li>
</ul>
<div class="footer">共1件商品<span>共计¥558.00</span> <span>去购物车</span></div>
</div>
</section>
<script>
function over(){
document.getElementById("cart").style.backgroundColor="#ffffff";
document.getElementById("cart").style.zIndex="100";
document.getElementById("cart").style.borderBottom="none";
document.getElementById("cartList").style.display="block";
document.getElementById("cartList").style.position="relative";
document.getElementById("cartList").style.top="-1px";
}
function out(){
document.getElementById("cart").style.backgroundColor="#f9f9f9";
document.getElementById("cart").style.borderBottom="solid 1px #dcdcdc";
document.getElementById("cartList").style.display="none";
}
</script>
</body>
</html>
示例8:随鼠标滚动的广告图片.scrollTop
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>随鼠标滚动的广告图片</title>
<style type="text/css">
#main{text-align:center; width:1014px; margin: 0 auto;}
#adver{
position:absolute;
left:10px;
top:30px;
z-index:2;
}
</style>
</head>
<body>
<div id="adver"><img src="images/adv.jpg"/></div>
<div id="main"><img src="images/main1.jpg"/><img src="images/main2.jpg"/><img src="images/main3.jpg"/></div>
<script>
var adverTop; //层距页面顶端距离
var adverLeft;
var adverObj; //层对象
function inix(){
adverObj=document.getElementById("adver"); //获得层对象
if(adverObj.currentStyle){
adverTop=parseInt(adverObj.currentStyle.top);
adverLeft=parseInt(adverObj.currentStyle.left);
}
else{
adverTop=parseInt(document.defaultView.getComputedStyle(adverObj,null).top);
adverLeft=parseInt(document.defaultView.getComputedStyle(adverObj,null).left);
}
}
function move(){
var sTop=parseInt(document.documentElement.scrollTop||document.body.scrollTop);
var sLeft=parseInt(document.documentElement.scrollLeft||document.body.scrollLeft);
adverObj.style.top=adverTop+sTop+"px";
adverObj.style.left=adverLeft+sLeft+"px";
}
window.οnlοad=inix;
window.οnscrοll=move;
</script>
</body>
</html>