html css 和 js 如何协同工作的

  • html css 和 js 如何协同工作的
  • 一个简单的登录样式的demo
  1. 在html中通过 <link /> 标签引入对应的css文件,例如:
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Log in</title>
    <link rel="stylesheet" type="text/css" href="login.css"/>
</head>

<link /> 标签写在<head>标签层,这样就将css样式文件链接了过来。

  1. 在html中通过<script ></script> 标签引入js文件,例如:
<body>
    <script type="text/javascript" src="login.js"></script>
</body>

<script ></script> 写在 <body> </body> 层,相当于将对应的目标文件加载到了这里。

html有了css样式文件,就增加了灵魂,就知道如何取渲染UI样式。js文件中写一些逻辑处理,例如点击事件的处理等,除此外,js中也可以加html。

以下是一个简单的登录功能的例子:

login.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Title -->
    <title>Log in</title>
    <!-- link css -->
    <link rel="stylesheet" type="text/css" href="login.css"/>
</head>
<body>
    <!-- login div -->
    <div class="login">
        <div class="login_nav">
            <h3>
                <span>Log in</span>
            </h3>
        </div>
        <div class="login_icon">
        </div>
        <div class="login_content">
            <div class="login_input">
                <input id="login_input_account" type="text" placeholder="User Name Or Email">
            </div>
            <div class="login_input">
                <input id="login_input_pwd" type="password" placeholder="Password">
            </div>
            <div class="login_button">
                <button onclick="loginBtnClick()">Log in</button>
            </div>
        </div>
    </div>
    <!-- load js -->
    <script type="text/javascript" src="login.js"></script>
</body>
</html>

login.css

.login{
    height:100%;
}


.login_nav{
    background: #1672c1;
    height: 44px;
    text-align: center;
    font-size: 16px;
}
h3{
    padding: 6px; 
}


.login_icon{
    height: 120px;
}




input[type="text"]{
    border: 1px solid lightgray;
    border-radius: 3px;
    box-sizing: border-box;
    width: 70%;
    height: 44px;
    text-indent: 1em;
    font-size: 14px;
    margin-left: 15%;
    margin-top: 16px;
}
input[type="password"]{
    border: 1px solid lightgray;
    border-radius: 3px;
    box-sizing: border-box;
    width: 70%;
    height: 44px;
    text-indent: 1em;
    font-size: 14px;
    margin-left: 15%;
    margin-top: 16px;
}

input[type="text"]::placeholder,input[type="password"]::placeholder{
    color: lightgray;
}
button{
    width: 70%;
    height: 44px;
    text-indent: 1em;
    font-size: 14px;
    margin-left: 15%;
    margin-top: 20px;
    background-color: #1672c1;
    border: 1px solid #1672c1;
}

login.js

loginBtnClick = () => {
    const a = document.getElementById("login_input_account").value;
    const p = document.getElementById("login_input_pwd").value;
    if (!a || !p) {
        alert("Please input login info!");
        return;
    }
    alert("login Btn Clicked");
};

将login.html, login.css, login.js 放一个文件中,点击login.html,就可以在浏览器打开这个web页面了,浏览器内核会负责将html css 和 js进行"解释",将对应标签和样式显示出来。

如果没有 css 的话,页面显示出来是这样的:

在这里插入图片描述
加了 css 样式修饰,是这样的
在这里插入图片描述
可以看出差距还是挺大的,css 可以说是 html 的灵魂,有了 css 的修饰,前端页面才会更加美观。

【See also】

[1] 菜鸟教程 - CSS 创建:https://www.runoob.com/css/css-howto.html

  • 2
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morris_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值