JS 获取 HTML DOM 元素的方法

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="box" class="box"></div>
    <div class="box">2</div>
</body>
<script>
    const box1 = document.getElementById("box");
    console.log("box1:", box1);
    const box2 = document.getElementsByTagName("div");
    console.log("box2:", box2);
    const box3 = document.getElementsByClassName("box");
    console.log("box3:", box3);
    const box4 = document.querySelector(".box");
    console.log("box4:", box4);
    const box5 = document.querySelectorAll(".box");
    console.log("box5:", box5);

    console.log("document.body:", document.body);
    console.log("document.documentElement:", document.documentElement);
</script>

</html>
相关推荐
西西学代码2 分钟前
Flutter---showCupertinoDialog
java·前端·flutter
你的眼睛會笑4 分钟前
vue3 使用html2canvas实现网页截图并下载功能 以及问题处理
前端·javascript·vue.js
ZTLJQ15 分钟前
植物大战僵尸HTML5游戏完整实现教程
前端·游戏·html5
无光末阳31 分钟前
vue 环境下多个定时器的创建与暂停的统一封装
前端·vue.js
Hilaku34 分钟前
技术Leader的“第一性原理”:我是如何做技术决策的?
前端·javascript·面试
liyf34 分钟前
发布-订阅(Publish–Subscribe) vs 观察者模式(Observer Pattern)
前端
云中雾丽40 分钟前
Flutter 里的 Riverpod 用法解析
前端
前端snow1 小时前
记录:非常典型的一个redux问题
前端
慧一居士1 小时前
src/App.vue 和 public/index.html 关系和区别
前端·vue.js
渣哥1 小时前
面试高频:Spring 事务传播行为的核心价值是什么?
javascript·后端·面试