原生js实现百度搜索功能

侯晓伟      2022-02-16     208

关键词:

  今天呢给大家分享一下自己用原生JS做的一个百度搜索功能,下面上代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <!--百度iocn图标-->
        <link rel="shortcut icon" href="https://www.baidu.com/favicon.ico" type="image/x-icon"/>
        <title>百度一下,你就知道</title>
        <style type="text/css">
            body{/*清除浏览器自带样式*/
                margin: 0;
                padding: 0;
            }
            .box{/*最大的盒子*/
                width: 100%;
                /*background: yellow;*/
                height: 636px;
                background-image:url("https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin/880.jpg?2");
                background-size: 100%;
                
            }
            .box_log{/*log盒子*/
                width: 100%;
            }
            
            .box_log img{/*百度log*/
                width: 19.8%;
                margin-left: 49.4%;
                transform: translate(-50%);
                margin-top: 38px;
                margin-bottom: 19px;
            }
            .box_text{/*text搜索框盒子大小*/
                width: 100%;
                height: 36px;
            }
            .log_img{/*input框中的小相机*/
                position: absolute;
                left: 865px;
                top: 202px;
            }
            #text{
                width: 540px;
                height: 36px;
                box-sizing: border-box;
                margin-left: 355px;
                margin-top: 3px;
                text-indent: 4px;
            }
            #btn{
                width: 100px;
                height: 36px;
                background: #3385FF;
                border: 0px;
                letter-spacing: 1px;
                color: white;
                margin-left: -5px;
                font-size: 15px;
                box-sizing: border-box;
                transform: translateY(2px);
                box-sizing: border-box;
            }
            #btn:hover{
                cursor: pointer;
            }
            #search{
                width: 540px;
                /*background: yellow;*/
                margin: 0;
                padding: 0;
                margin-left: 355px;
                list-style: none;
                display: none;
                border: 1px solid #E3E5E4; 
            }
            #search li{
                line-height: 36px;
                background: white;
            }
            #search li:hover{
                background: #F0F0F0;
            }
            .li1{
                text-indent: 4px;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="box_log">
                <img src="../img/superlogo_c4d7df0a003d3db9b65e9ef0fe6da1ec.png"/>
            </div>
            <div class="box_text">
                <img src="../img/QQ图片20180119115441.png" class="log_img"/>
                <input type="text" name="text" id="text" value=""/>
                <input type="button" name="bdyx" id="btn" value="百度一下" />
                <ul id="search">
                    <li class="li1" id="0" onclick="iptShow(this.id)"></li>
                    <li class="li1" id="1" onclick="iptShow(this.id)"></li>
                    <li class="li1" id="2" onclick="iptShow(this.id)"></li>
                    <li class="li1" id="3" onclick="iptShow(this.id)"></li>
                    <li class="li1" id="4" onclick="iptShow(this.id)"></li>
                    <li class="li1" id="5" onclick="iptShow(this.id)"></li>
                    <li class="li1" id="6" onclick="iptShow(this.id)"></li>
                    <li class="li1" id="7" onclick="iptShow(this.id)"></li>
                    <li class="li1" id="8" onclick="iptShow(this.id)"></li>
                    <li class="li1" id="9" onclick="iptShow(this.id)"></li>
                </ul>
            </div>
        </div>
        
        
        <script>
            var otext = document.getElementById("text");  //获取input框
            ose = document.querySelector("#search");  //通过类名选择器 选择到search框
            lis = document.getElementsByClassName("li1");  //获取所有的li
            otext.onkeyup = function(){   //当在input框中键盘弹起发生事件
                ose.style.display = otext.value?"block":"none";  /*三目运算符,如果otext.value的值部位空,则block。*/
                var osc = document.createElement("script");  /*创建一个script标签*/
                osc.src = "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd="+otext.value+"&cb=houxiaowei";
                /*srcipt的src值引入百度的url,然后将otext文本框中输入的内容连接到url,在后面在运行自己的方法*/
                document.body.appendChild(osc);
                /*将创建好的script标签元素放入body中*/
                
                
                /*input框中按下回车根据input的值跳转页面*/
                if(event.keyCode==13){
                    /*将百度作为连接,传入input的值,并跳入新的页面*/
                    window.location.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd="+otext.value
                }
            }
            
            var count = 0;
            var search = 0;
            var arr = ose.children; /*获取ose下的所有li*/
            function houxiaowei(json){
                var jsonLength = 0;  /*json长度的初始值*/
//                    console.log(json.s);  打印json数据中的所有数据
                for(var x in json.s){   /*将循环的次数变成json的长度*/
                    jsonLength++;     
                }
//                    console.log(jsonLength);  打印json数据的长度
                for(var i=0;i<lis.length;i++){
                    if(jsonLength==0){  /*如果遍历出的长度等于0,li的值为空*/
                        arr[i].innerHTML = null;   
                    }else{
                        if(json.s[i]!=null){/*如果json[i]的值不等于空,则将它的值放入li中*/
                            arr[i].innerHTML = json.s[i];
                        }
                    }
                }
                if(count==lis.length-1){
                    count=0;
                    search=0;
                }
                count++;
                search++;
            }
            
            /*单击li中的值显示在input框中*/
            function iptShow(thisId){
                otext.value = arr[thisId].innerHTML;
                window.location.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd="+otext.value
            }
            /*单击body中的任意地方隐藏li*/
            document.body.onclick = function(){
                ose.style.display = "none";
            }
        
            /*单击百度btn的时候触发,并跳到新的连接*/
            var btn = document.querySelector("#btn");
            btn.onclick = function(){
                /*获取当前input的值*/
                var otext = document.getElementById("text").value;
                /*将百度作为连接,传入input的值,并跳入新的页面*/
                window.location.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd="+otext
            }  
        </script>
    </body>
</html>

 

原生js模拟百度搜索关键字与跳转

css:ul{  display:none;}html:<inputtype="text"id="text"/><ulid="list"></ul>JS:<script>  varoTxt=document.getElementById("text");  varoUl=document.getElementById("list");  oTxt.o 查看详情

rxjs实现“搜索”功能

闲言少叙,直接开冲!先用JS原生写一个搜索功能:<inputid="text"></input><script>vartext=document.querySelector(#text);text.addEventListener(keyup,(e)=>varsearchText=e.target.value;$.ajax(//发送请求url:`searc 查看详情

百度百科tooltip的实现--原生js的应用

我们在浏览百度百科时,不难发现提示框的存在,如下图:实现如下:1、HTML代码部分<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Tooltip提示框</title><metaname="keywords"content=""><m 查看详情

原生js实现删除class和添加class

内容来自百度搜索//判断样式是否存在functionhasClass(ele,cls){    returnele.className.match(newRegExp("(\s|^)"+cls+"(\s|$)"));}//为指定的dom元素添加样式functionaddClass(ele,cls){    if(! 查看详情

原生js实现vue组件功能

...情况下,我们一直在惊叹于VUE的组件的功能,却不知道,原生js早就已经支持了这个功能。最近在公开课学到的,js还有很多很多需要探索学习。下面是一个简单的例子<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title... 查看详情

百度地图实现普通地图定位周边搜索功能

注意:此Demo是用AndroidStudio实现的//--------------一下是普通地图的实现-----------------------------------1:打开百度地图APi进去,如果没有注册过的需要注册,注册过的登录进入2:点击开发——Android地图SDk——点击获取密钥——点击创... 查看详情

原生js实现拖动功能

代码:1functiondrag(id){23varobj=document.getElementById(id),4resultX=0,5resultY=0;67functiongetPos(t){8varoffsetLeft=0,9offsetTop=0,10offsetParent=t;1112while(offsetParent){13offsetLeft+=offsetParent.of 查看详情

原生js实现查询天气的小应用

demo:https://zsqosos.github.io/weather/截图:实现功能:打开网页时显示用户所在城市的天气状况,在输入框输入城市可查询其它城市。实现过程:先调用百度地图的API来获取用户所在的城市,随后调用聚合数据的天气API将数据放在页... 查看详情

原生js实现查询天气的小应用

demo:https://zsqosos.github.io/weather/截图:实现功能:打开网页时显示用户所在城市的天气状况,在输入框输入城市可查询其它城市。实现过程:先调用百度地图的API来获取用户所在的城市,随后调用聚合数据的天气API将数据放在页... 查看详情

vue+antd搭配百度地图实现搜索定位等功能(代码片段)

...中又引入了百度地图,所以我就打算通过使用百度地图来实现地址搜索功能啦。本次教程可能过于啰嗦,所以这里先放上预览地址供大家预览——点我预览,也可到文末直接下载代码先自行体验。。。ps:又因为百度地图1.2以上需... 查看详情

原生js实现购物车相关功能

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>实现购物车的相关功能</title><style>*{margin:0;padding:0;}table{width:600px;border-collapse:collapse;}th,t 查看详情

原生js实现table表格的各行变色功能

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>原生js实现各行变色</title><style>*{margin:0;padding:0;}#studentCore{width:300px;margin:0auto;}table{bor 查看详情

原生js实现简易评论更新功能

<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title></title><styletype="text/css">#outside{width:1000px;margin:0auto;border:1pxsolid#E7EAEE;overflow:hidden; 查看详情

c#中如何实现百度搜索框的效果

参考技术Ac#本身没这个功能,如果是b/s项目的话需要用html+css+js等前端代码实现,如果是c/s项目则需要使用wpf或者winform 查看详情

百度搜索功能

<!DOCTYPEhtml><htmllang="en"ng-app="myApp"><head> <metacharset="UTF-8"> <title>百度搜索</title> <scriptsrc="js/angular.min.js"></script>  查看详情

原生js实现ajax的文件异步提交功能图片预览功能.实例

<%--CreatedbyIntelliJIDEA.User:yhDate:2016/12/14Time:17:13TochangethistemplateuseFile|Settings|FileTemplates.--%><%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@includef 查看详情

模拟百度

...索的一个东西,获取百度接口,利用jsonp获取百度数据,实现百度框搜索的功能! 我是用jq来些的 ,我们先引入jq。 <scriptsrc="jquery.min.js>还有我们封装的一个jsonp的一个js 这只是个插件 直接引入即可<scrip... 查看详情

原生js写一个功能强大的编辑器(代码片段)

...大佬能对此文章及本人写的代码,不吝赐教。主要采用了原生JS与调用SelectionAPI结合html的contentible功能实现功能。1.先来看看效果2.主要功能1.在当前光标位置添加指定图片,并=点 查看详情