Javascript- DOM对象

  • DOM 简介
  • DOM 操作HTML
  • DOM 操作CSS
  • DOMEventListener(句柄)

DOM简介

dom.png

DOM 操作HTML

DOM操作HTML.png
操作HTML.png
<body>
    <p id="pid">hello</p>
    <button onclick="demo()" >annniu</button>
    <script>
        function demo() {
            document.getElementById("pid").innerHTML = "nidaye";
            
        }

    </script>

<body>

    ![](1.png)
    <a id="aid" href="http://www.baidu.com">hello</a>
    <button onclick="demo()" >annniu</button>
    <script>
        function demo() {
            document.getElementById("aid").href ="http://www.app-echo.com";
          document.getElementById("aid").innerHTML = "echo";
          document.getElementById("imgid").src ="2.jpg";
        }

    </script>


通过DOM操作CSS

1.通过DOM对象改变CSS 语法:document.getElementById(id).style.property = new style

<body>

    <div id="id">
      hell0
    </div>
    <button onclick="demo()">按钮</button>
    <script>
    function demo() {
        document.getElementById("id").style.color ="blue";

    }
    </script>

DOMEventListener

方法:
addEventListener(): 方法用于向指定元素添加事件句柄
removeEventListener():移除方法添加的事件句柄

<body>

    <p id="pid">Hello</p>
    <button id="btn"  >按钮</button>
    <script>

        document.getElementById("btn").addEventListener("click",function () {
            alert("hello");
        })
      var x = document.getElementById("btn");
      x.addEventListener("click",hello);//句柄
      x.addEventListener("click",world);
      x.removeEventListener("click",hello);
        function hello() {
            alert("hello")
        }
        function world() {
            alert("world")
        }
    </script>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • DOM简介 当网页加载时,浏览器会创建页面的文档对象模型(DOM)。通过DOM,JS可以对HTML实现以下操作:改...
    figure_ai阅读 3,486评论 0 1
  • HTML DOM 当页面被加载时,浏览器会创建文档对象模型(Document Obiect Model) JsDO...
    Adapa阅读 2,405评论 0 0
  • 介绍 一.获取document元素 二.给document元素添加事件 三.对document节点增删改成 简书:...
    lioilwin阅读 2,695评论 0 0
  • 以下文章为转载,对理解JavaScript中的事件处理机制很有帮助,浅显易懂,特分享于此。 什么是事件? 事件(E...
    jxyjxy阅读 8,159评论 1 10
  • 没有深夜痛哭的人,不足以谈人生,而我觉得, 我离可以谈人生的日子不远了。 开始找工作了,也开始一段自己决定的路了,...
    Lasia阅读 2,286评论 0 2