DOM初探系列(2)

DOM初探

三种节点

  • 元素节点
  • 属性节点
  • 文本节点

nodeValue属性

  • node.nodeValue是获取属性节点对应属性的值

nodeType属性

+node. nodeType 是节点的类型,元素节点的nodeType = 1,属性节点的nodetype = 2, 文本节点的nodeType = 3。当节点不存在时nodeType= null

childNodes属性

  • 获取节点的所有子节点,返回值为数组形式

firstChild 属性

  • 获取第一个子节点,和node.childNodes[0]等价

lastChild属性

  • 获取最后一个子节点,和node.childNodes[document.getElementByTagName(“li”).length-1]等价

最后通过代码来深入了解这几个属性的用法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Image gallery</title>
    <style type="text/css">
        body{
            font-family: "Helvetica","Arial",sans-serif;
            color: #333;
            background-color: lightblue;
            margin: 1em 10%;
        }
        h1{
            color: #333;
            background-color: transparent;
        }
        a{
            color: #c60;
            background-color: transparent;
            font-weight: bold;
            text-decoration: none;
        }
        ul{
            padding: 0;
        }
        li{
            float: left;
            padding: 1em;
            list-style: none;
        }
    </style>
    <script type="text/javascript">
        function showPic(whichPic) {
            var placeHolder = document.getElementById('placeholder');
            var source = whichPic.getAttribute('href');
            placeHolder.setAttribute('src', source);


            var text = whichPic.getAttribute('title');
            var description = document.getElementById('description');
            description.firstChild.nodeValue = text;
        }

    </script>
</head>
<body>
<h1>snapshots</h1>
<ul>
    <li>
        <a href="pictures/tree.png" onclick="showPic(this);return false" title="firework display">
            Fireworks</a>
    </li>
    <li>
        <a href="pictures/home.png" onclick="showPic(this);return false" title="coffee display">
            Coffee</a>
    </li>
    <li>
        <a href="pictures/timg.jpeg" onclick="showPic(this);return false" title="rose display">
            Rose</a>
    </li>
    <li>
        <a href="pictures/31_379_bp.jpg" onclick="showPic(this);return false" title="big ben display">
            Big Ben</a>
    </li>
</ul>
<img id="placeholder" src="pictures/state.jpg" alt="my image gallery"/>
<p id="description">choose an image</p>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值