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>
相关推荐
w***Q35022 分钟前
前端跨平台开发工具,Tauri与Electron
前端·javascript·electron
前端一课22 分钟前
H5 WebView 文件下载到手机中(仅安卓与 iOS)
前端
幸会同学30 分钟前
在Cesium中实现飘动的红旗
javascript·three.js·cesium
天外来物1 小时前
element-plus主题配置及动态切换主题
前端·css·element
flypwn1 小时前
justCTF 2025JSpositive_player知识
开发语言·javascript·原型模式
晴殇i1 小时前
这个前端工具杀疯了!发布一周狂揽 10k Star,Snapchat 开源框架重新定义跨平台
前端·程序员
孟祥_成都2 小时前
打包票!前端和小白一定明白的人工智能基础概念!
前端·人工智能
小满zs2 小时前
Next.js第六章(平行路由)
前端
孤狼warrior2 小时前
公司信息建设库数据 使用调用堆栈的JS逆向爬虫
javascript·爬虫
小满zs2 小时前
Next.js第七章(路由组)
前端