JS 事件与 DOM 对象

1、JS 事件

1.1、作用:

  • 验证用户输入的数据

  • 增加页面的动感效果

  • 增强用户的体验度

1.2、事件的绑定

  • 在标签在添加 onclick = ""

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <button onclick="func_click()">按钮1</button>
        
        <script>
            function func_click() {
                console.log("我被点击了!");
            }
        </script>
    </body>
</html>
  • 选中元素,在 js 代码中为元素绑定事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <button id="btn">按钮2</button>
        
        <script>
            var btn = document.querySelector("#btn");
            btn.onclick = function() {
                console.log("我被点击了!");
            }
        </script>
    </body>
</html>

1.3、事件类型

1.3.1、鼠标事件

  • onclick:鼠标单击事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <button onclick="func_click()">按钮1</button>
        
        <script>
            function func_click() {
                console.log("我被点击了!");
            }
        </script>
    </body>
</html>
  • ondblclick:鼠标双击事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <button ondblclick="func_dblclick()">按钮2</button>
        
        <script>        
            function func_dblclick() {
                console.log("我被双击了!");
            }
        </script>
    </body>
</html>
  • onmouseover:鼠标移入事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <button onmouseover="func_over()">按钮3</button>
        
        <script>
            function func_over() {
                console.log("我移入了!");
            }
        </script>
    </body>
</html>
  • onmouseout:鼠标移出事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <button onmouseout="func_out()">按钮4</button>
        
        <script>
            function func_out() {
                console.log("我移出了!");
            }
        </script>
    </body>
</html>
  • onmousemove:鼠标移动事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <button onmousemove="func_move()">按钮5</button>
        
        <script>        
            function func_move() {
                console.log("我移动了!");
            }
        </script>
    </body>
</html>

1.3.2、键盘事件

  • onkeyup:键盘松开事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <input type="text" onkeyup="func_up()">
        
        <script>
            function func_up() {
                console.log("我被松开了!")
            }
        </script>
    </body>
</html>
  • onkeydown:键盘按下事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <input type="text" onkeydown="func_down()">
​
        <script>
            function func_down() {
                console.log("我被按下了!")
            }
        </script>
    </body>
</html>

1.3.3、表单事件

  • onfocus:获取焦点事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <input type="text" onfocus="func_focus()">
        
        <script>
            function func_focus() {
                console.log("我获取到了焦点!");
            }
        </script>
    </body>
</html>
  • onblur:失去焦点事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <input type="text" onblur="func_blur()">
        
        <script>
            function func_blur() {
                console.log("我失去了焦点!");
            }
        </script>
    </body>
</html>

1.3.4、文档事件

  • onload:页面加载事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body onload="func_load()">
            function func_load() {
                console.log("页面加载完毕!");
            }
        </script>
    </body>
</html>

2、DOM

2.1、介绍

        DOM:Document Object Model 文档对象模型

        作用:赋予了我们操作页面中元素的能力,实现对元素的新增,创建,修改,删除……

        加载 HTML 页面时,Web 浏览器生成一个树型结构,用来表示页面内部结构。DOM 将这种树型结构理解为由节点组成,组成一个节点树

2.2、节点的分类

  • 文档节点 文档本身 整个文档 document

  • 元素节点 所有的HTML元素 <a>、<div>、<p>

  • 属性节点 HTML元素内的属性 id、href、name、class

  • 文本节点 元素内的文本 hello

  • 注释节点 HTML中的注释 <!-- -->

2.3、节点的操作

2.3.1、获取节点

  • 直接获取

    • getElementById():根据元素的 id 属性值获取一个节点

    • getElementsByClassName():根据元素的 class 属性值获取一组节点

    • getElementsByTagName():根据元素的标签名获取一组节点

    • getElementsByName():根据元素的 name 属性值获取到一组节点

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <div id="box">
            box
            <p class="haha">box 中的 haha</p>
        </div>
        <p class="haha">p1</p>
        <p class="haha">p2</p>
        <span class="haha">span</span>
​
        <form action="" id="form">
            <input type="checkbox" name="hobby" value="1"> 读书
            <input type="checkbox" name="hobby" value="2"> 写字
            <input type="checkbox" name="hobby" value="3"> 敲代码
        </form>
        
        <script>
            // 根据元素的 id 属性值获取一个节点
            var box = document.getElementById("box");
            console.log(box);
            
            // 根据元素的 class 属性值获取一组节点
            var hahas = document.getElementsByClassName("haha");
            console.log(hahas);
            
            // 根据元素的标签名获取一组节点
            var ps = document.getElementsByTagName("p");
            console.log(ps);
            
            // 根据元素的 name 属性值获取到一组节点
            var hobby = document.getElementsByName("hobby");
            console.log(hobby);
        </script>
    </body>
</html>
  • 间接获取

    • childNodes:返回元素的一个子节点的数组

    • firstChild:返回元素的第一个子节点

    • lastChild:返回元素的最后一个子节点

    • nextSibling:返回元素的下一个兄弟节点

    • parentNode:返回元素的父节点

    • previousSibling:返回元素的上一个兄弟节点

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <ul id="box">
            <li class="first">li1</li>
            <li>li2</li>
            <li>li3</li>
            <li>li4</li>
            <li>li5</li>
        </ul>
​
        <script>
            var box = document.getElementById("box");
​
            console.log(box.childNodes);
            console.log(box.firstChild);
            console.log(box.firstElementChild);     
            console.log(box.lastChild);
            console.log(box.lastElementChild);
            console.log(box.firstElementChild.nextSibling);
            console.log(box.firstElementChild.nextElementSibling);
            console.log(box.lastElementChild.previousSibling);
            console.log(box.lastElementChild.previousElementSibling);
            console.log(box.parentNode);
            console.log(box.firstElementChild.parentNode);
            
        </script>
    </body>
</html>

2.3.2、创建节点

  • 创建元素节点

var div = document.createElement("div");
  • 创建文本节点

var text = document.createTextNode("我是文本节点!");
  • innerHTML

div.innerHTML = "明天的你会感谢今天努力的自己!";
div.innerHTML = "<p>我是p标签</p>"

2.3.3、插入节点

  • 父节点.appendChild(子节点):把子节点追加到父节点的最后

var box = document.querySelector("#box")
box.appendChild(div);
  • 父节点.insertBefore(要插入的子节点, 参考节点)

var p = document.createElement("p");
p.innerHTML = "明天的你会感谢今天努力的自己!";
box.insertBefore(p, div);

2.3.4、操作节点

  • 替换节点

    • 父节点.replaceChild(新子节点, 原子节点)

    • 返回被替换的节点

    • 如果新节点是页面中原有的节点,实现了移动覆盖的效果

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <ul>
            <li>li1</li>
            <li>li2</li>
            <li>li3</li>
            <li>li4</li>
        </ul>
        
        <script>
            var ul = document.getElementsByTagName("ul")[0];
            var newLi = document.createElement("li");
            newLi.innerHTML = "新的 li 标签";
​
            console.log(ul.replaceChild(newLi, ul.firstElementChild));
            console.log(ul.replaceChild(ul.lastElementChild, ul.firstElementChild));
        </script>
    </body>
</html>
  • 删除节点

    • 父节点.removeChild(子节点)

    • 返回被删除的节点

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <ul>
            <li>li1</li>
            <li>li2</li>
            <li>li3</li>
            <li>li4</li>
        </ul>
        
        <script>
            console.log(ul.removeChild(ul.firstElementChild));
        </script>
    </body>
</html>
  • 克隆节点

    • 节点.cloneNode(boolean)

    • true:深拷贝,拷贝当前节点以及子元素

    • false:浅拷贝,拷贝当前节点不拷贝字内容

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <ul>
            <li>li1</li>
            <li>li2</li>
            <li>li3</li>
            <li>li4</li>
        </ul>
        
        <script>
            console.log(ul.cloneNode(true));
            console.log(ul.cloneNode(false));
        </script>
    </body>
</html>
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值