3JavaScript操作DOM对象


#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{margin0;padding0;}

ul,li{list-stylenone;}

body{

    font-family"微软雅黑";

    font-size12px;

    line-height28px;

}

#news{

    border1px solid #cccccc;

    width280px;

    overflowhidden;

    margin5px auto 0 auto;

}

#news header{

    border-bottom1px solid #cccccc;

    font-size16px;

    line-height40px;

    padding-left:15px;

    color#666666;

    font-weightbold;

}

#news header a{text-decorationnonecolor#666666floatrightpadding-right10pxfont-size12px;}

#news ul li{padding-left10px;}

#news ul li a{color#686868text-decorationnonedisplayinline-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-size12pxfont-family"Arial""微软雅黑"line-height25px;}

        div{padding5pxtext-aligncenter;

        }

        div span{displayblock;}

    </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-size12pxfont-family"Arial""微软雅黑"line-height25px;}

        div{padding5pxtext-aligncenter;

        }

        div span{displayblock;}

    </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:删除和替换节点removeChildreplaceChild

自定义del、rep

<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title>删除和替换节点</title>

    <style>

        *{padding0margin0font-size14px;}

        ul,li{list-stylenone;}

        li{floatlefttext-aligncenterwidth140px;}

    </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

*{margin0;padding0font-family"Arial""微软雅黑"font-size12pxline-height25px;}

ul,li{list-stylenone;}

#shopping{margin20px auto 0 autowidth320px;

}

#cart{

    background#f9f9f9 url("../images/cart.png"20px 6px no-repeat;

    bordersolid 1px #dcdcdc;

    floatright;

    width100px;

    height28px;

    padding-left45px;

    line-height28px;

    positionrelative;

    cursorpointer;

}

#cart span{

    positionabsolute;

    color#fff;

    background#dc1742;

    displayblock;

    width15px;

    height15px;

    border-radius50%;

    top:-5px;

    right20px;

    font-size8px;

    line-height15px;

    text-aligncenter;

}

#cartList{width310pxfloatrightbordersolid 1px #dcdcdcdisplaynone;}

#cartList h2{border-bottom1px dashed #ccccccfont-size14pxpadding-left10px;}

#cartList li{floatleft;}

#cartList li:nth-of-type(1){width65pxtext-aligncenter;}

#cartList li:nth-of-type(2){width155px;}

#cartList li:nth-of-type(3){text-aligncenterwidth85px;}

#cartList .footer{clearbothheight35pxline-height35pxbackground#f5f5f5padding:0  5px;}

#cartList .footer span{padding0 12px;}

#cartList .footer span:nth-of-type(2){

    color#fff;

    background#dc1742;

    displayblock;

    height25px;

    border-radius6px;

    floatright;

    text-aligncenter;

    font-weightbold;

    margin-top5px;

}

#shopping .cartOver{

    background-color#ffffff;

    z-index100;

    border-bottomnone;

}

#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:centerwidth:1014pxmargin0 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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值