CSS和JavaScript

CSS

在html中引入CSS

我们需要先在该项目先建立css文件

html引入CSS,在<head></head>中添加<link>标签

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>
    <!-- 引入外部的css -->
    <link rel="stylesheet" href="../css/demo1.css">
</head>
<body>
    <div>哈哈哈</div>
    <div>酷酷酷</div>
</body>
</html>

CSS选择器:选取设置样式的元素(标签)

分类:

1.元素选择器

元素名称(color:red) 如: div{color:red}

调用:<div>hello</div>

2.id选择器

#id属性值(color:red) 如:#name{color:red}

调用:<div id="name">hello</div>

3.类选择器

.class属性值(color:red) 如:.cls{color:red}

调用: <div class="cls">hello</div>

CSS的属性:

border属性

border-style的值

html 复制代码
 #mydiv1
{
    color: red;
    /* 设置字体大小为20px,注意,在css中如果需要加单位,单位不能省略 */
     font-size: 20px;
     border-width: 10px;
     border-style: dashed;
}

结果:

display属性

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>
    <!-- 引入外部CSS文件 -->
    <link rel="stylesheet" type="text/css"  href="../css/demo2.css">
</head>
<body>
    <div>css1</div>
    <div id="mydiv1">css2</div>
    <div class="cla">css3</div>
    <div style="display: none;">css4</div>
    <div style="display: inline;">css5</div>
    <div style="display :inline">css6</div>
    <div style="display: block;">css7</div>
</body>
</html>

javaScript

javascript的引入方式

1.内部脚本:将JS代码定义在HTML页面中,使用<script>标签

2.外部脚本:将JS代码定义在JS文件中,然后引入到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>
    <!-- 在html页面内部引入js -->
    <script>
        //弹窗
        alert("你好呀")
    </script>

    <!-- 引入外部js文件 -->
    <script src="../js/demo1.js"></script>
    <!-- 注意在引入外部文件后,不能再文本中写js代码 -->
</body>
</html>

demo1.js文件

html 复制代码
alert("哈哈哈")

javascript的输出方式

使用window.alert();写入警告框

使用document.write();写入HTML页面

使用console.log();写入浏览器控制台

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>
        alert("哈哈")
        // 写入html页面
        document.write("赫赫")
        // 写入浏览器控制台
        // f12打开浏览器控制台
        console.log("你好")
    </script>
</body>
</html>
相关推荐
cooldream200913 分钟前
华为云Flexus+DeepSeek征文|基于华为云Flexus X和DeepSeek-R1打造个人知识库问答系统
人工智能·华为云·dify
老胖闲聊3 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1183 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之4 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
DFminer5 小时前
【LLM】fast-api 流式生成测试
人工智能·机器人
lyaihao5 小时前
使用python实现奔跑的线条效果
python·绘图
郄堃Deep Traffic5 小时前
机器学习+城市规划第十四期:利用半参数地理加权回归来实现区域带宽不同的规划任务
人工智能·机器学习·回归·城市规划
ai大师5 小时前
(附代码及图示)Multi-Query 多查询策略详解
python·langchain·中转api·apikey·中转apikey·免费apikey·claude4
GIS小天6 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票
小小爬虾6 小时前
关于datetime获取时间的问题
python