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>
相关推荐
光泽雨3 小时前
c#中的Type类型
开发语言·前端
Captaincc3 小时前
来自 Codex 官方团队的分享:如何把 Codex 用到极致
前端·vibecoding
lichenyang4534 小时前
鸿蒙聊天 Demo 练习 05:新增登录功能,实现登录态保存与页面访问控制
前端
还有多久拿退休金4 小时前
我用 Three.js 造了个 3D 漫步世界,角色走路像喝醉了——以及我是怎么修好的
前端·vue.js
SZLSDH4 小时前
场景适配论 | 数字孪生IOC建设中渲染技术与智能体能力的协同逻辑
前端·数据库·ai·数字孪生·数据可视化·智能体
_按键伤人_5 小时前
二、从零搭建本地 RAG 知识库
前端·llm·ai编程
_按键伤人_5 小时前
一、理解 RAG:从概念到实践
前端·llm·ai编程
lichenyang4535 小时前
鸿蒙聊天 Demo 练习 04:聊天历史本地缓存,实现消息记录持久化
前端
名字都不重要何况昵称5 小时前
canvas 元素拾取
前端·canvas
从文处安5 小时前
「前端何去何从」React Router:让单页应用有多页的体验
前端·react.js