JavaScript DOM操作

DOM全称 Document Object Model,即文档对象模型,它允许脚本(js)控制Web页面、窗口和文档。

关于DOM更详细的说明

下面主要是关于如何用DOM操作对象改变样式或者属性。

1.DOM操作HTML
(1)改变HTML输出流:
注意不要在文档加载完成之后使用document.write()。会覆盖该文档。
(2)寻找元素:
通过id找到HTML元素。
通过标签名找到HTML元素。
(3)改变HTML内容:
使用属性innerHTML
(4)改变HTML属性:
使用属性:attribute

典型的例子:

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p>修改html的内容</p>
        <p id="pid">Hello,world!</p>
        <button onclick="change1()">修改p便签的内容</button>
        <script>
            function change1(){
                document.getElementById("pid").innerHTML = "HELLO,WORLD!";
            }
        </script>

        <p>修改html属性</p>
        <a id="aid" href="https://www.baidu.com">百度首页</a><br>
        <button onclick="change2()">修改链接属性</button>
        <script>
            function change2(){
                document.getElementById("aid").href = "https://www.qq.com/";
                document.getElementById("aid").innerHTML = "腾讯首页";
            }
        </script>
    </body>
</html>

其他方法:

在这里插入图片描述
(1)getElementsByName()通过name获取元素。

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <script>
            function getName(){
                var count = document.getElementsByName("pn");
                alert(count.length);//得到4。说明得到一个数组集合 
                var p = count[0];
                p.innerHTML = "World";//将第一个p标签的内容进行修改
            }
            getName();
        </script>
    </body>
</html>

(2)getAttribute()获取元素属性。

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>

        <a id="aid" title="得到了a标签的属性">hello</a>
        <script>
            function getArr(){
                var anode = document.getElementById("aid");
                var atrr = anode.getAttribute("title");
                alert(atrr);
            }
            getArr();
        </script>
    </body>
</html>

(3)setAttribute()设置属性。

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>

        <a id="aid" title="得到了a标签的属性">hello</a>
        <script>
            function setArr(){
                var anode = document.getElementById("aid");
                anode.setAttribute("href","https://www.baidu.com");
            }
            setArr();
        </script>
    </body>
</html>

(4)childNodes访问子节点。

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>

        <a id="aid" title="得到了a标签的属性">hello</a>
        <ul><li>1</li><li>2</li><li>3</li></ul>
        <script>
            function getChildNode(){
                var childnode = document.getElementsByTagName("ul")[0].childNodes;
                alert(childnode.length);
            }
            getChildNode();
        </script>
    </body>
</html>

(5)parentNode访问父节点,只有一个。

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>

        <a id="aid" title="得到了a标签的属性">hello</a>
        <ul><li>1</li><li>2</li><li>3</li></ul>
        <div id="div"><p id="pid"></p></div>
        <script>
            function getParentNode(){
                var div = document.getElementById("pid");
                alert(div.parentNode.nodeName);
            }
            getParentNode();
        </script>
    </body>
</html>

(6)creatElement()创建元素节点

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <a id="aid" title="得到了a标签的属性">hello</a>
        <ul><li>1</li><li>2</li><li>3</li></ul>
        <div id="div"><p id="pid"></p></div>
        <script>
            function creatNode(){
                var body = document.body;
                var input = document.createElement("input");
                input.type = "button";
                input.value = "按钮";
                body.appendChild(input);
            }
            creatNode();
        </script>
    </body>
</html>

(7)insertBefore()插入节点

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <a id="aid" title="得到了a标签的属性">hello</a>
        <ul><li>1</li><li>2</li><li>3</li></ul>
        <div id="div"><p id="pid"></p></div>
        <script>
            function addNode(){
                var div = document.getElementById("div");
                var node = document.getElementById("pid");
                var newNode = document.createElement("p");
                newNode.innerHTML = "动态添加一个p元素";
                div.insertBefore(newNode,node);
            }
            addNode();
        </script>
    </body>
</html>

(8)removeChild()删除节点

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <a id="aid" title="得到了a标签的属性">hello</a>
        <ul><li>1</li><li>2</li><li>3</li></ul>
        <div id="div">
            <p id="pid1">第一个p元素</p>
        </div>
        <script>
            function removeNode(){
                var div = document.getElementById("div");
                var p = div.removeChild(div.childNodes[1]);
            }
            removeNode();
        </script>
    </body>
</html>

(9)网页尺寸OffsetHeight和scrollHeight

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <p name="pn">Hello</p>
        <a id="aid" title="得到了a标签的属性">hello</a>
        <ul><li>1</li><li>2</li><li>3</li></ul>
        <div id="div">
            <p id="pid1">第一个p元素</p>
        </div>
        <script>
            function getSize(){
                var height = document.documentElement.offsetHeight||document.body.offsetHeight;
                var width = document.documentElement.offsetWidth||documrnt.body.offsetWidth;
                alert(width + "," + height);
            }
            getSize();
        </script>
    </body>
</html>

2.通过DOM对象改变CSS样式。
基础语法:

document.getElementById(id).style.property = new style;

实际例子:

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <style>
        .div{
            width: 100px;
            height: 100px;
            background: lightskyblue;
        }
    </style>
    <body>
        <div class="div" id="div">hello world</div>
        <button onclick="change1()">改变背景颜色</button>
        <button onclick="change2()">改变字体大小</button>
        <button onclick="change3()">改变字体颜色</button>
        
        <script>
            function change1(){
                document.getElementById("div").style.backgroundColor = "red";
            }
            function change2(){
                document.getElementById("div").style.fontSize = "20px"
            }
            function change3(){
                document.getElementById("div").style.color = "white";
            }
        </script>
    </body>
</html>

3.DOM EventListener
(1)addEventListener():
语法:element.addEventListener(event, function, useCapture);
方法用于向指定元素添加时间句柄。
(2)removeEventListener():
移除方法添加事件句柄。

<html>
    <head>
        <meta charset="utf-8">
    </head>
    
    <body>
        <button id="btn">按钮</button>
        <script>
            var x = document.getElementById("btn");
            x.addEventListener("click",hello);
            x.addEventListener("click",world);

            // 删除EventListener
            // x.removeEventListener("click",hello);
            // x.removeEventListener("click",world);
            
            function hello(){
                alert("hello");
            }
            function world(){
                alert("world");
            }
        </script>
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值