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>
相关推荐
独立开发之道几秒前
【Three.js教程】 图元速查:官方内置的立体图形
开发语言·javascript·ecmascript
程序员小八777几秒前
后端转全栈:前端思维转变(Vue 视角)
前端·vue.js·状态模式
RD_daoyi3 分钟前
谷歌改写了76%的标题:超60字符的,95%会被谷歌自己重写
大数据·服务器·开发语言·前端·搜索引擎·html
IMPYLH11 分钟前
HTML 的 <map> 元素
前端·html
宿67426 分钟前
vue3-vite
前端·vue.js
风骏时光牛马30 分钟前
云原生驱动模型工具高效落地与规模化赋能
前端
zhanghaha131431 分钟前
HTML系列教程:8_HTML 文本格式化标签
前端·css·html
梦想的旅途237 分钟前
基于企业微信API的微应用前端与后端架构设计
前端·状态模式·企业微信
JavaGuide2 小时前
一个 SKILL.md 拿下 2.3 万 Star:让 Codex/Claude Code 少说废话、先给答案
前端·后端
晴天162 小时前
多个 VS Code 项目会导致 Chrome 和 Electron 应用一起卡住?-Day30
前端·chrome·electron