zoukankan      html  css  js  c++  java
  • js仿百度搜索框

    1.js仿百度搜索框

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title></title>
        <script>
          function show(json){
            let oUl=document.getElementById('ul1');
            oUl.innerHTML='';
            json.s.forEach(str=>{
              let oLi=document.createElement('li');
              oLi.innerHTML=str;
              oUl.appendChild(oLi);
            })
          }
        </script>
        <script>
          window.onload=function(){
            let oTxt=document.getElementById('txt1');
            let oUl=document.getElementById('ul1');
    
            oTxt.oninput=function(){
              let oS=document.createElement('script');
              oS.src=`https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=${oTxt.value}&cb=show`;
              document.head.appendChild(oS);
            }
          }
        </script>
      </head>
      <body>
    <input type="text" id="txt1">
    <ul id="ul1">
    
    </ul>
      </body>
    </html>

    2.jq仿百度搜索框

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title></title>
        <script type="text/javascript" src="jquery.js">
        </script>
        <script>
          $(function(){
            $('#txt1').on('input',function(){
              $.ajax({
                url:'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
                data:{wd:$('#txt1').val()},
                dataType:'jsonp',
                jsonp:'cb',
                success(json){
                  $('#ul1').html('');
                  json.s.forEach(str=>{
                    $(`<li>${str}</li>`).appendTo($('#ul1'));
                  })
                },
                error(){
                  alert('错了')
                }
              })
            })
          })
        </script>
      </head>
      <body>
    <input type="text" id="txt1">
    <ul id="ul1">
    
    </ul>
      </body>
    </html>
  • 相关阅读:
    1074 食物链 (并查集)
    2832 6个朋友
    病毒 (拓扑)
    4735 烦人的幻灯片 (拓扑)
    JavaScript中变量的LHS引述和RHS引用
    td自动换行
    SQL Server 中的 NOLOCK 到底是什么意思?
    jQuery中遇到的坑
    jQuery中attr()函数 VS prop()函数
    Javascript刷新页面的几种方法
  • 原文地址:https://www.cnblogs.com/chaofei/p/7857933.html
Copyright © 2011-2022 走看看