欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery Jsonp跨域模拟搜索引擎

程序员文章站 2022-03-21 14:58:37
效果还不错 就差加上键盘控制了... 代码如下: ...

效果还不错 就差加上键盘控制了...

jQuery Jsonp跨域模拟搜索引擎

代码如下:

<!doctype html> 
<html> 
  <head> 
    <meta charset="utf-8" /> 
    <title>迷糊网罗</title> 
    <style type="text/css"> 
      * { 
        margin: 0; 
        padding: 0; 
      } 
      form { 
        position: absolute; 
        left: 50%; 
        top: 40%; 
      } 
      form input:nth-child(1) { 
        width: 450px; 
        height: 34px; 
        float: left; 
        font-size: 16px; 
        text-indent: 0.5em; 
        outline: none; 
        box-sizing: border-box; 
      } 
      form input:nth-child(1):focus { 
        border: 1px solid blueviolet; 
      } 
      form input:nth-child(2) { 
        height: 34px; 
        float: left; 
        background: blueviolet; 
        cursor: pointer; 
        width: 80px; 
        letter-spacing: 2px; 
        border: 0; 
        font-size: 14px; 
        line-height: 34px; 
      } 
      #oul { 
        position: absolute; 
        left: 0; 
        top: 34px; 
        background: white; 
        width: 530px; 
        box-shadow: 3px 3px 5px #f3f3f3; 
        border: 1px solid #f3f3f3; 
        box-sizing: border-box; 
        display: none; 
      } 
      #oul li:hover { 
        background: #f3f3f3; 
        cursor: pointer; 
      } 
    </style> 
  </head> 
  <body> 
    <form action="" method="" name="ss"> 
      <input type="" name="ss" id="txt" value="" /> 
      <input type="button" name="ss" id="btn" value="迷糊网罗" /> 
      <ul id="oul"> 
      </ul> 
    </form> 
    <script type="text/javascript"> 
      //https://sp0.baidu.com/5a1fazu8aa54nxgko9wtanf6hhy/su?wd=&cb=&_=1497256697565 
      //位置调整 习惯就好.. 
      var fm = document.getelementsbytagname('form')[0]; 
      var x = fm.offsetwidth; 
      var y = fm.offsetheight; 
      fm.style.marginleft = -x / 2 + 'px'; 
      fm.style.margintop = -y / 2 + 'px'; 
      //获取操作元素 
      var txt = document.getelementbyid("txt"); 
      var oul = document.getelementbyid("oul"); 
      //动态创建js脚本 
      txt.onkeyup = function(ev) { 
        var value = this.value; 
        var newscript = document.createelement("script"); 
        newscript.src = 'https://sp0.baidu.com/5a1fazu8aa54nxgko9wtanf6hhy/su?wd=' + value + '&cb=walk&_=' + math.random(); 
        newscript.type = 'text/javascript'; 
        document.body.appendchild(newscript); 
      } 
      //设置回调函数 
      function walk(walkjson) { 
        if(txt.value == 0) { 
          oul.style.display = 'none'; 
        } else { 
          oul.style.display = 'block'; 
          oul.innerhtml = ''; 
          if(walkjson.s.length <= 10) { 
            for(var i = 0; i < walkjson.s.length; i++) { 
              var oli = document.createelement('li'); 
              oli.style.height = 25 + 'px'; 
              oli.style.width = 100 + '%'; 
              oli.style.lineheight = 25 + 'px'; 
              oli.style.textindent = 9 + 'px'; 
              oli.style.liststyle = 'none'; 
              oli.innerhtml = walkjson.s[i]; 
              oul.appendchild(oli); 
            } 
          } else { 
            for(var i = 0; i < 10; i++) { 
              var oli = document.createelement('li'); 
              oli.style.height = 25 + 'px'; 
              oli.style.width = 100 + '%'; 
              oli.style.lineheight = 25 + 'px'; 
              oli.style.textindent = 9 + 'px'; 
              oli.style.liststyle = 'none'; 
              oli.innerhtml = walkjson.s[i]; 
              oul.appendchild(oli); 
            } 
          } 
        } 
      } 
    </script> 
  </body> 
</html> 

以上所述是小编给大家介绍的jquery jsonp跨域模拟搜索引擎的实例代码,希望对大家有所帮助