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>
相关推荐
网络点点滴8 分钟前
Vue3路由params参数
前端·javascript·vue.js
hqk11 分钟前
鸿蒙 ArkUI 从零到精通:基础语法全解析
android·前端·harmonyos
wordbaby38 分钟前
React Native (Expo) iOS 真机调试失败排查:xcodebuild exited with error code 65
前端·react native
今天也很困1 小时前
解决浏览器后台定时器降频问题:用 Worker 实现高精度 setInterval
前端
只与明月听1 小时前
一次uniapp问题排查
前端·javascript·vue.js
Bacon1 小时前
Vitest 一个基于 Vite 的快速单元测试框架
前端
学不动学不明白1 小时前
AES-GCM 解密失败解决方案
前端
一水鉴天1 小时前
整体设计 定稿 之16 三层智能合约体系实现设计和开发的实时融合
前端·人工智能·架构·智能合约
捕捉一只前端小白1 小时前
前端面试题(仅供参考)
html·css3·js
小菜今天没吃饱1 小时前
DVWA-XSS(Reflected)
前端·xss·dvwa