2019-08-29_Work_Day29

Ajax 解梦&垃圾分类 接口调用

<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <title>Ajax</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .top {
            width: 80%;
            height: 100px;
            position: relative;
            top: 200px;
            left: 100px;
            text-align: center;
            font: 18px/30px 'courier new';
        }

        #func{
            font: 18px/30px 'courier new';
        }

        #word {
            text-align: center;
            font: 18px/30px 'courier new';
            border: none;
            outline: none;
            border-bottom: 1px solid gray;
        }

        #search {
            width: 60px;
            height: 40px;
            font: 18px/30px 'courier new';
        }

        #container {
            width: 80%;
            height: 100%;
            position: relative;
            top: 250px;
            left: 100px;
            text-align: center;
            font: 18px/30px 'courier new';
        }
    </style>

</head>

<body>
    <div class="top">
        <select id="func">
            <option value="0" selected>周公解梦</option>
            <option value="1">垃圾分类</option>
        </select>
        <input type="text" id="word">
        <button id="search">查询</button>
    </div>
    <div id="container"></div>


</body>
<script src="../js/jquery.min.js"></script>
<script>
    $(() => {
        const container = $('#container')
        // const key = 'f82260eff0d31183dd10d8327a148b98'
        // const num = 10
        const URLs = [
            'http://api.tianapi.com/txapi/dream/',
            'http://api.tianapi.com/txapi/lajifenlei/'
        ]
        const clsssedList = ['可回收垃圾', '有害垃圾', '湿垃圾', '干垃圾']
        const selected = $('#func')
        const input = $('#word')
        let selectIndex = selected.val()
        let selectURL = URLs[selectIndex]

        function doAjaxSearch() {
            let word = input.val().trim()
            if (word.length <= 0) {
                input.val('')
                return
            }
            container.empty()

            $.ajax({
                url: selectURL,
                type: 'get',
                data: {
                    key: 'Your Key',
                    num: 10,
                    word: input.val()
                },
                dataType: 'json',
                headers: {

                },
                success: (json) => {
                    let results = json.newslist
                    if (selectIndex == 0) {
                        for (res of results) {
                            container.append(
                                $('<div>').html(res.result)
                            )
                        }
                    } else if (selectIndex == 1) {
                        for (res of results) {
                            container.append($('<p>').text(`${res.name}:  ${clsssedList[res.type]}`))
                        }
                    }
                },
                error: (err) => {}
            })
            input[0].focus()
        }

        selected.on('change', (evt) => {
            selectIndex = selected.val()
            selectURL = URLs[selectIndex]
            input.val('')
            container.empty()
        })

        input.on('keyup', (evt) => {
            if (evt.keyCode == 13) {
                doAjaxSearch()
            }
        })

        $('#search').on('click', doAjaxSearch)
    })
</script>

</html>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、简历准备 1、个人技能 (1)自定义控件、UI设计、常用动画特效 自定义控件 ①为什么要自定义控件? Andr...
    lucas777阅读 10,637评论 2 54
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,872评论 1 32
  • 1.abstract class 和interface 有什么区别? 抽象类可以有构造方法 接口不行 抽象类可以有...
    sunnysans阅读 4,365评论 0 1
  • 上午 mysore练习,我练习的比较快,全套练习完是一个半小时,当然倒立还是没做。 老师辅助的比较少,是双脚式c,...
    马蹄儿嘚嘚阅读 1,531评论 0 0
  • 既然是来到了公交公司培训“老司机”的学校,那就不能不写一下我的班主任老师了。这可是位狠角色。 总共就一年半制的职业...
    北峰阅读 1,792评论 0 5

友情链接更多精彩内容