【web】JavaScript——Web APIs笔记小结 Web APIs+dom部分(Web APIs和DOM简介)

  争取卷起来啊喂,之前真的好躺qwq   

来源:JavaScript基础语法

目录

I.Web APls

① Web APIs 和 JS 基础的关联性

​编辑② API 和 Web API

II.DOM

① 简介​编辑

② 获取元素

※※ 根据ID 获取

​编辑※※ 根据标签名获取

※※ 通过HTML5新增的方法获取(i9以上版本支持)

※※ 特殊元素获取

③ 事件基础

※※ 三要素

​编辑

※※ 执行过程

④ 操作元素

※※ 修改普通元素

​编辑

※※※※ 普通案例

※※ 修改表单属性

※※※※ 表单案例

※※ 修改样式属性

​编辑 ※※※※ 样式案例

※※※※ 循环精灵图

※※ 文本框案例


I.Web APls

Web APIs 和 JS 基础的关联性

② API 和 Web API

地址戳这 → MDN

II.DOM

① 简介

② 获取元素

※※ 根据ID 获取

getElementByld() 方法可以获取 带有ID 的 元素对象 

※※ 根据标签名获取

getElementsByTagName() 方法可以返回 带有指定标签名 的 对象的集合 

※※ 通过HTML5新增的方法获取(i9以上版本支持)

※※ 特殊元素获取

③ 事件基础

※※ 三要素

btn.onclick = function() {
    alert('点秋香');
}

※※ 执行过程

④ 操作元素

※※ 修改普通元素

※※※※ 普通案例

  图可以自己随便找,这里就不放了 

<img src="w.jpg" alt="早上好!">
<div>早上好</div>
<script>
    var img = document.querySelector(' img ');
    var div = document.querySelector('div ');
    var time = new Date();
    var h = time.getHours();
    if (h < 12) {
        img.src = "z.jpg";
        img.alt = "早上";
        div.innerHTML = '早安 ';
    } else if (h < 18) {
        img.src = "x.jpg";
        img.alt = "下午";
        div.innerHTML = '下午好 ';
    } else {
        img.src = "w.jpg";
        img.alt = "晚";
        div.innerHTML = '晚好 ';
    }
</script>

※※ 修改表单属性

※※※※ 表单案例

 html和css复习戳这里 → html汇总css边框css定位

我的:

<!DOCTYPE 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>Document</title>
    <style>
        .box {
            position: relative;
            width: 600px;
            height: 50px;
            margin: 50px auto;
            border: 10px inset dodgerblue;
        }
        
        input {
            width: 470px;
            height: 45px;
            outline: 0;
        }
        
        input::-ms-reveal {
            display: none;
        }
        /* 从网上学的去掉小眼睛的方法 */
        
        img {
            position: absolute;
            top: 0;
            right: 0;
        }
    </style>

    <body>
        <div class="box">
            <img src="2.jpg">
            <input type="password">
        </div>
        <script>
            var img = document.querySelector('img');
            var pw = document.querySelector('input');
            var flag = 0;
            img.onclick = function() {
                if (flag == 0) {
                    img.src = "1.jpg";
                    pw.type = 'text';
                } else {
                    img.src = "2.jpg";
                    pw.type = 'password';
                }
                flag = !flag;
            }
        </script>
    </body>

</html>

 PS. 例图是用ps拼起来的

pink的:

<!DOCTYPE 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>Document</title>
    <style>
        .box {
            width: 400px;
            border-bottom: 1px solid#ccc;
            margin: 100px auto;
        }
        
        .box input {
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
        }
    </style>
</head>

<body>
    <div class="box">
        <label for="">
            <img src="images/close.png" alt="" id="eye">
        </label>
        <input type="password" name="" id="pwd"></div>
    <script>
        //1.获取元素
        var eye = document.getElementById('eye');
        var pwd = document.getElementById('pwd');
        //2.注册事件处理程序
        var flag = 0;
        eye.onclick = function() { //点击一次之后,flag一定要变化
            if (flag == 0) {
                pwd.type = 'text';
                eye.src = 'images/open.png';
                flag = 1;
            } else {
                pwd.type = 'password';
                eye.src = 'images/close.png';
                flag = 0;
            }
        }
    </script>

</body>

</html>

※※ 修改样式属性

 ※※※※ 样式案例

我的: 

<!DOCTYPE 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>Document</title>
    <style>
        .box {
            position: relative;
            width: 445px;
            height: 440px;
            margin: 50px auto;
            border: 10px inset dodgerblue;
        }
        
        .btn {
            position: absolute;
            top: 13%;
            left: 20px;
            width: 30px;
            height: 30px;
            border: 3px solid slateblue;
            border-radius: 10px;
            text-align: center;
        }
        
        .img {
            position: absolute;
            top: 50%;
            margin-top: -190px;
            padding: 5px;
            right: 20px;
            width: 350px;
            height: 377px;
            border: 3px solid slateblue;
            border-radius: 10px;
        }
        
        p {
            margin: 3px;
            text-align: center;
            font-size: 15px;
        }
        
        img {
            width: 350px;
            height: 350px;
            border-radius: 20px;
        }
    </style>

    <body>
        <div class="box">
            <button class="btn">X</button>
            <div class="img">
                <p>图来自百度~!</p>
                <img src="1.jpg">
            </div>
        </div>
        <script>
            var btn = document.querySelector('.btn');
            var box = document.querySelector('.box');

            btn.onclick = function() {
                box.style.display = 'none';
            }
        </script>
    </body>

</html>

PS.点了按钮好几次都没反应。。然后发现是把  document.querySelector('.btn'); 写成了  getElementsByClassName ,如果你也忘了的话赶快去看本篇的获取元素哦!

pink的:

<!DOCTYPE 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>Document</title>
    <style>
        .box {
            position: relative;
            width: 74px;
            height: 88px;
            border: 1px solid#ccc;
            margin: 100px auto;
            font-size: 12px;
            text-align: center;
            color: #f40;
            /* display: block; */
        }
        
        .box img {
            width: 60px;
            margin-top: 5px;
        }
        
        .close-btn {
            position: absolute;
            top: -1px;
            left: -16px;
            width: 14px;
            height: 14px;
            border: 1px solid#ccC;
            line-height: 14px;
            font-family: Arial, Helvetica, sans-serif;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="box">淘宝二维码
        <img src="images/tao.png" alt="">
        <i class="close-btn">x</i>
    </div>
    <script>
        //1.获取元素
        var btn = document.querySelector('.close-btn');
        var box = document.querySelector('.box');
        //2.注册事件程序处理
        btn.onclick = function() {
            box.style.display = 'none;'
        }
    </script>

</body>

</html>

※※※※ 循环精灵图

 这个是直接抄的pink的 

<!DOCTYPE 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>Document</title>

</head>

<body>
    <div class="box">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <script>
        //1.获取元素所有的小li
        var lis = document.querySelectorAll('li');
        for (var i = 0; i < lis.length; i++) {
            //让索引号乘以44就是每个li的背景y坐标
            var index = i * 44;
            //index就是我们的y坐标
            lis[i].style.backgroundPosition = '0 -' + index + 'px';
        }
    </script>

</body>

</html>

※※ 文本框案例

我的: 

<!DOCTYPE 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>Document</title>
    <style>
        .box {
            position: relative;
            margin: 50px auto;
            width: 270px;
            border: 10px groove dodgerblue;
            padding: 10px;
            border-radius: 10px;
        }
        
        input {
            color: #999;
            width: 260px;
            font-size: 16px;
            font-family: '楷体';
            font-weight: 700;
            height: 24px;
            border: 3px solid #999;
            border-radius: 6px;
        }
        
        button {
            position: absolute;
            height: 32px;
            width: 50px;
            margin-right: -4px;
            top: 20%;
            right: 5%;
            border: 3px solid #999;
            background-color: lightskyblue;
            border-radius: 6px;
            color: rgb(123, 122, 122);
            font-size: 16px;
            font-weight: 700;
            font-family: '楷体';
        }
    </style>
</head>

<body>
    <div class="box">
        <input type="text" value="手机">
        <button>搜索</button>

    </div>
    <script>
        var text = document.querySelector('input');
        text.onfocus = function() {
            if (text.value == '手机') text.value = '';
        }
        text.onblur = function() {
            if (text.value == '') text.value = '手机';
        }
    </script>

</html>

 只放了显示的图,可以直接复制代码试一下ww

pink的: 

<!DOCTYPE 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>Document</title>

</head>

<body>
    <input type="text" value="手机">
    <script>
        //1.获取元素
        var text = document.querySelector('input');
        // 2.注册事件获得焦点事件 onfocus
        text.onfocus = function() {
                //console.log('得到了焦点');
                if (this.value === '手机') this.value = '';
                //获得焦点需要把文本框里面的文字颜色变黑
                this.style.color = '#333';
            }
            //3.注册事件失去焦点事件 onblur
        text.onblur = function() {
            //console.log('失去了焦点');
            if (this.value == '') this.value = '手机';
            //失去焦点需要把文本框里面的文字颜色变浅色
            this.style.color = '#999';
        }
    </script>

</body>

</html>

  恭喜看到这的小伙伴,你已经完成JavaScript APIs第一天的学习了~!!

下面进入第一天剩余部分的学习吧 (。・∀・)ノ゙ → 样式属性部分案例

 有用的话可以点赞评论收藏哦!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

进击的文文文

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

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

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

打赏作者

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

抵扣说明:

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

余额充值