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>
相关推荐
Penge66626 分钟前
《Go 后端开发者学 TypeScript:export关键字大全》
javascript
Penge66634 分钟前
《Go 后端开发者学 TypeScript:从零理解 async、await 与 Promise》
javascript
Penge6661 小时前
typescript 中const用法
javascript
Csvn1 小时前
Monorepo
前端
Csvn1 小时前
流水线
前端
Csvn1 小时前
微前端架构
前端
Linux-lucky3 小时前
45-Linux学习之旅之防火墙与WEB加固
linux·前端·网络·学习·ubuntu
风骏时光牛马5 小时前
生成一个移动端标题,只需要一个标题,不需要其他的任何文本。
前端
IT_陈寒6 小时前
Redis雪崩把我坑惨了,这是5年经验的教训
前端·人工智能·后端
SameX6 小时前
HarmonyOS ArkUI List 的侧滑删除 用 ListItem 的 swipeAction + endIcon 的做法
前端