jQuery版本百度建议

内容介绍

  两年前的demo,没存货了发一下,支持上下方向键选择,iframe嵌入百度搜索页面。

一、直接上代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>百度建议</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .search-wp {
            width: 500px;
            margin: 50px auto;
        }
        
        .search-wp>.search-common>input,
        button {
            width: 80%;
            height: 40px;
            outline: none;
            line-height: 40px;
            box-sizing: border-box;
            float: left;
        }
        
        .search-wp button {
            width: 20%;
            border: 0px;
            background: skyblue;
            border-radius: 0px 5px 5px 0px;
        }
        
        .search-wp>.search-common::after {
            content: '';
            clear: both;
            display: block
        }
        
        .search-wp>.search-content {
            width: 80%;
            box-sizing: border-box;
            border: 1px solid #ccc;
            display: none;
        }
        
        .search-wp>.search-content li {
            list-style: none;
            height: 35px;
            line-height: 35px;
            border: 1px solid #ccc;
        }
        
        .search-wp>.search-content li.on {
            background-color: darkslateblue
        }
        
        .web-wp {
            width: 100%;
            display: flex;
            justify-content: center;
        }
        
        iframe {
            width: 800px;
            height: 500px;
            border: 1px solid blue;
            position: absolute;
            top: 200px;
        }
    </style>
</head>

<body>
    <div class="search-wp">
        <div class="search-common">
            <input type="text" placeholder="请输入搜索内容">
            <button>开始 搜索</button>
        </div>
        <div class="search-content">
            <ul>
            </ul>
        </div>
    </div>
    <div class="web-wp">
        <iframe src="" frameborder="0" scrolling="no"></iframe>
    </div>
    <script>
        // 百度建议:

        $('.search-wp input[type=text]').on('keyup', ongetContnetEvent)
            // input输入框绑定键盘事件ongetContentEvent
        function ongetContnetEvent(e) {

            let val = $(this).val();
            // 获取input框中的值
            if (val == '') {
                $('.search-wp .search-content').hide()
                return
                // 如果input框中无值,content中的搜索内容不显示
            }
            // 点击回车按钮进行跳转,提取class为on值的li标签内容,作为location.href值
            if (e.keyCode == 13) {
                let val = $('.search-wp ul li.on').html()
                    // location.href = `https://www.baidu.com/s?wd=${val}`
                let iframeHref = `https://www.baidu.com/s?wd=${val}`
                if (!iframeHref) {
                    $('iframe').attr('src', 'https://www.baidu.com')
                } else {
                    $('iframe').attr('src', iframeHref)
                }

            }

            if (e.keyCode == 38 || e.keyCode == 40) {
                let index = $('.search-wp ul li.on').index()
                    // 获取class值为on的索引值
                if (e.keyCode == 38) {
                    --index
                    // 点击向上方向键时,索引值减小
                } else {
                    index = ++index % $('.search-wp ul li').length;

                }
                $('.search-wp ul li')
                    .eq(index).addClass('on')
                    .siblings('.on')
                    .removeClass('on');
                // 对应索引值的li添加on值,同级元素中有on值的删除class
                $('.search-wp input').val($('.search-wp ul li.on').html())
                    // 将class值为on的文本添加至input框
                return
            }
            getData(val)
        }

        function getData(val) {
            $.get("https://www.baidu.com/sugrec?cb=?", {
                    prod: 'pc',
                    wd: val
                },
                function(res) {
                    if (!Object.keys(res).length)
                    // 返回值response中无内容的,直接返回
                        return
                    res.g = res.g || [];
                    console.log(res.g)
                        // res.g有内容的使用原内容,无内容的赋值空数组,防止传值报错
                    $('.search-wp ul')
                        .html(renderSearchList(res.g))
                        .parent()
                        .slideDown()
                        // 将渲染值动态添加至ul中,其父级.search-content及内容显示
                },
                "json")
        }

        function renderSearchList(data) {

            if (data.length === 0) {
                return ('<li class="">查询无结果</li>')
            } else {
                return data.map((item, index) => {
                    return `<li class="${index==0?'on':''}">${item.q}</li>`
                }).join('')
            }
            // res.g传参,无内容的显示“无查询结果”,有内容的将数组中的每项值遍历转为字符串
        }
    </script>
	<script>
        console.log("%c ", 'margin-left:0px;padding:100px 100px;background-image: url("https://liujianwei695.gitee.io/minifile/images/officialAccount.jpg");background-size: 200px 200px;background-position: center;background-repeat: no-repeat;');
    </script>
</body>

</html>
二、效果演示

百度建议效果图
点击进入:百度建议演示地址

只作为单一测试demo,不做后续补充,有兴趣的小伙伴可粘贴代码自行完善。


happy的事就要一起分享:

在这里插入图片描述
在这里插入图片描述


标签:jQuery,HTML,百度建议,&


更多演示案例,查看 案例演示


欢迎评论留言!

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 17
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

人间小美味695

您的鼓励是我创作的动力,感谢!

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

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

打赏作者

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

抵扣说明:

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

余额充值