JavaScript(四)DOM及CSS操作

1、DOM简介

DocumentType: Html的声明标签

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>
    <SCript>
        console.log(document);   // document代表整改文档
        if(document.nodeType ===9){
            console.log("顶层节点");
        }
    </SCript>
</body>
</html>

2、document对象获取元素






3、document对象创建元素


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 class="nav">导航</div>
    <div id="contaniner"></div>

    <script>
        //创建元素
        var p_text = document.createElement("p");
        console.log(p_text);
        // 创建文本信息
        var p_content = document.createTextNode("Js创建文本信息");
        // appendChild:将内容或者子元素放到容器中
        p_text.appendChild(p_content);
        console.log(p_text);
        //创建属性
        var P_id = document.createAttribute("id");
        // 给属性赋值
        P_id.value = "darly";
        console.log(P_id);
        // 将属性设置到相应标签中
        p_text.setAttributeNode(P_id);
        console.log(p_text);

        //将创建的元素显示在页面上
        //首先获取需要放置的父级元素
        var contaniner = document.getElementById("contaniner");
        console.log(contaniner);
        contaniner.appendChild(p_text);
    </script>
</body>
</html>

4、Element属性




InnerHTML可以识别标签

innerText会把标签识别成一个字符串

5、获取元素位置


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>
    <style>
        .box{
            width: 200px;
            height: 200px;
            border: 5px solid red;
            padding: 10px;
            margin: 20px;
            background-color: green;
        }
        h1{
            height: 500px;
        }
    </style>
</head>
<body>
    <div class="box" id="boxid"></div>
    <h1>标题内容1</h1>
    <h1>标题内容2</h1>
    <h1>标题内容3</h1>
    <h1>标题内容4</h1>
    <h1>标题内容5</h1>
    <h1>标题内容6</h1>
    


    <script>
        //获取元素
        var boxobj = document.getElementById("boxid")
        //获取元素大小
        console.log(boxobj.clientHeight); //打印元素高度  包括pading值220(200+10+10)
        console.log(boxobj.clientWidth);  //打印元素宽度,包括pading值220(200+10+10)
        //获取视口高度(屏幕高度)
        console.log(document.documentElement.clientHeight);

        //获取网页总高度(页面高度,指页面内容高度,空白部分不算页面高度
        console.log(document.body.clientHeight);
        console.log("===clientHeight及clientWidth与scrollHeight和scrollWidth区别不大,\
                        实际应用场景中也很少将内容隐藏=========== ");


        console.log(boxobj.scrollHeight); //打印元素高度  包括pading值220(200+10+10)
        console.log(boxobj.scrollWidth);  //打印元素宽度,包括pading值220(200+10+10)
        //获取视口高度(屏幕高度)
        console.log(document.documentElement.scrollHeight);

        //获取网页总高度(页面高度,指页面内容高度,空白部分不算页面高度
        console.log(document.body.scrollHeight);
        
        console.log("================================");
        //scrollLeft和scrollTop
        //获取滚动高度
        console.log(document.documentElement.scrollTop);
        console.log("================================");

        //offsetHeight、offsetWidth
        console.log(boxobj.offsetHeight); //打印元素高度  包括pading和border值230(200+2*10+2*5)
        console.log(boxobj.offsetWidth);  

        console.log("================================");
        //offsetHeight、offsetWidth
        console.log(boxobj.offsetLeft); //左边距距离有定位的父级元素距离(有定位父级元素是指父级元素有position属性定义
        console.log(boxobj.offsetTop);  //上边距距离有定位的父级元素距离
    </script>

</body>
</html>

6、css操作(js操作css)


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>
    <!-- <style>
        .box{
            width: 200px;
            height: 200px;
            background-color: red;
        }
    </style> -->
</head>
<body>
    <div class="box" id="boxid"></div>

    <script>
        var box= document.getElementById("boxid")
        //setAttribute操作CSS属性
        box.setAttribute("style","width:200px;height:200px;background:red")

        //通过元素节点的style属性设置
        box.style.width = "300px";
        box.style.height = "300px";
        box.style.backgroundColor = "red";

        //通过cssText设置
        box.style.cssText = "width:200px;height:200px;background:red";
    </script>
</body>
</html>
相关推荐
Joy T4 小时前
Agent 确定性上下文设计:从 Prompt 参数注入到能力分层
java·开发语言·微服务·架构·prompt·agent context
半个落月7 小时前
LangSmith 全链路观测(下):用 Dataset 与 LLM Judge 量化评估 RA
javascript·langchain
专注仿真7 小时前
引擎扩展性与集成设计深度剖析:从 14 个扩展点到 DIS→HLA 演进之路
开发语言·算法·仿真引擎·推演引擎
Best-Wishes7 小时前
科来网络分析系统(技术交流版)使用说明书
开发语言·junit·php
半个落月7 小时前
LangSmith 全链路观测(上):用 LangGraph 与 Milvus 构建可追踪的 RAG 客服
javascript·langchain
代码不停8 小时前
自动化测试(java)
java·开发语言·selenium·自动化
半个落月8 小时前
LangChain.js Agent 中间件实战:生命周期、动态工具与文件权限
javascript·langchain
程序员大阳9 小时前
副队长CSS教程(33)–圆角与透明
前端·css·透明·圆角
志尊宝9 小时前
Vue3 零基础每日笔记(082):文件上传实战——el-upload 校验、自定义上传、头像预览
前端·javascript·vue.js·笔记·#javascript·#vue.js
鬼手点金10 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github