微博热搜的制作

在这里插入图片描述

<!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>
    <style>
        .big {
            width: 600px;
            height: 500px;
            margin: auto;

        }

        #inp {
            position: absolute;
            width: 600px;
            height: 35px;
            border: 1px solid #ccc;
            margin-top: 10px;
            border-radius: 10px;
            text-indent: 2em;
            font-size: 14px;


        }

        #but {
            position: relative;
            left: 530px;
            top: 2px;
            width: 70px;
            height: 35px;
            border: 0px;
            margin-top: 10px;
            background-color: white;
            border-radius: 2px;
        }

        .show {
            margin-top: 20px;
            width: 600px;
            height: auto;
            border: 1px solid #ccc;
            border-radius: 10px;
            display: none;

        }

        a {
            height: 35px;
            line-height: 35px;
            width: 600px;
            display: inline-block;
            text-indent: 1em;
            font-weight: 60px;
            font-size: 16px;
            text-decoration: none;
            color: black;
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
        }


        a:hover {
            color: orange;
            background-color: #ccc;
        }

        span {
            width: 70px;
            height: 35px;
            line-height: 35px;
            float: right;
            vertical-align: top;
        }

        li {
            width: 30px;
            height: 35px;
            line-height: 35px;
            float: left;
            vertical-align: top;
            list-style: none;
        }

        h1 {
            width: 200px;
            margin: auto;
        }
    </style>
    <div class="big">
        <h1>微博 ‧ 热搜榜</h1>
        <input id="inp" type="text" placeholder="">
        <input id="but" type="button" value="搜索">
        <div id="keyshow" class="show"></div>
        <!--使用原生js写一网页 -->
    </div>

    <script>
        //步骤一:创建异步对象
        //什么是 XMLHttpRequest 对象?
        // XMLHttpRequest 对象用于在后台与服务器交换数据。
        // XMLHttpRequest 对象是开发者的梦想,因为您能够:
        // 在不重新加载页面的情况下更新网页
        // 在页面已加载后从服务器请求数据
        // 在页面已加载后从服务器接收数据
        // 在后台向服务器发送数据
        var ajax = new XMLHttpRequest();
        //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
        ajax.open('get', 'https://tenapi.cn/resou/', true);
        //步骤三:发送请求
        ajax.send();
        //步骤四:注册事件 onreadystatechange 状态改变就会调用
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4 && ajax.status == 200) {
                //步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的    
                var data = JSON.parse(ajax.responseText);

                //以下函数为获取数组长度,然后循环将数据渲染到页面
                for (var i = 0; i < data.list.length; i++) {
                    //创建一个a标签用来显然数据条
                    var a = document.createElement('a');
                    // 将数据添加到标签中,并创建子标签,注意子标签的写法,要用()包含起来
                    a.innerHTML = ("<li>" + (i + 1) + "." + "</li>") + data.list[i].name + ("<span>" + ((data.list[i].hot) / 10000).toFixed(0) + "万" + "</span>");
                    // 将a标签添加链接
                    a.href = data.list[i].url;
                    // HTML DOM appendChild() 方法可向节点的子节点列表的末尾添加一个新的子节点。、
                    // 这里是循环添加a标签到页面中
                    document.getElementById('keyshow').appendChild(a);
                    // 获取标签id
                    var inp = document.getElementById('inp');
                    //给inp标签的placeholder属性添加动态的数据
                    inp.placeholder = "大家都在搜:  " + data.list[0].name;
                }
                //显示热搜框,在数据获取渲染完之后显示
                document.getElementById('keyshow').style.display = 'block';
            }
        }
        // 添加一个函数,当键盘按下时,触发事件
        document.getElementById('inp').onkeyup = function () {
            // 获取输入框的值
            var val = document.getElementById('inp').value;
            // 获取a标签存储在a数组中
            var a = document.getElementsByTagName('a');
            // 创建一个数组,用来存储搜索到的数据
            for (var i = 0; i < a.length; i++) {
                // indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
                // 如果没有找到匹配的字符串,则返回 -1。
                //匹配到则显示该条记录
                if (a[i].innerHTML.indexOf(val) != -1) {
                    a[i].style.display = 'block';
                } else {
                    a[i].style.display = 'none';
                }
            }
        }
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

张有泽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值