Ajax跨域实现淘宝/百度搜索下拉提示效果

时间:2022-05-27 11:21:40

最近学到Ajax,觉得自己对与前后端的数据交互有了一个基本的了解。下面是Ajax应用到淘宝/百度的搜索功能的一个简单的小实例,就是输入一个词,下拉框中自动显示匹配的内容:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXsAAAHrCAIAAAChOqofAAAT1UlEQVR4nO3dL5PdVp7HYb2LhoGBgWYTGBgYGGgY6AoKNHRVNjupRSGTCTQMSplNLzMwMDQIMDQ0WmuBE/u2/hydo3uv3F/peaprqlute6Seqt8n0u1O1PUAW+k+9QkAB6I4wHYUB9iO4gDbURxgO4oDbGe2OC/+fPfv/+19+PBxbz9e/Pluy1hcxHRxXvz57vsf//j1px98+PBxbz++//GPuOhMF+fX23f/+q8ftj0ToM2vP/3wr//836c+izaKA6kUB9iO4gDbURxgO4oDbEdxgO0oDrAdxQG2ozjAdhQH2I7ijFbv/LvpcC2KM1pdceBqFGe0uuLA1SjOaHXFgatRnNHqigNXs//ivH3523dffX7Tdd3N5189/Pn2zYfvvLn9+eH773Q3n3/z+OnLt32vOHBNey/Om98f3tw8fPq67/u+f/304U337dM3fd/3b58/ftB9/fPL91+8/v27L7ovnjzvFQeuae/FuX3cdV//9mq099tn33Xdd8/eDnZ8rThwTXsvztvnjx90N19+++S321dvR6958/L22dNfnjx6+PWDzzrFgavbe3H6vn/z8umTh1991nVd99nHN3Le3j7+8qb77MHXDx/9/Nuzl2+ePVIcuLoDFOdvb1/f/vLdlzfdV7+86vtXv3zVffHo9uSuSnHg+g5UnL7v+/7Zo657fPv+bZuHv38MzvMnXygOXN3ei/P8yRfd1z8//+tO6tVv39zcPHr2tu/fPP22u/nm/VvKb1///uhB13Xvf1mlOHA9ey9O379+9uTbL9//1c1nD759/Purv7a/uX3yzcc/03n28u/fnCsOXM/+i9O8uuLA1SjOaHXFgatRnNHqigNXozjAdhQH2I7iANtRHGA7igNsR3GA7SgOsB3FAbajOMB2FAfYjuIA21EcYDv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rD/F2K8Sdze55uqSkaHI7iTBxg/rZosiCaArUU587SJ0HpRm8An35STpKrG5imOKUj3X1LuLxb+XOg7xVn7hgVlzanOy9+DvT93ouz+Fuq8R3QOCv1X859S3rgL/suzrlH+vuuanGfyd0UB4YUZ+Fga3PzYbu3kOEjxQG2EzikigOxAodUcSBW4JAqDsQKHFLFgViBQ6o4ECtwSBUHYgUOqeJArMAhVRyIFTikigOxAodUcSBW4JAqDsQKHNJicXz48HHPP9LknTGQS3GA7SgOsB3FAbajOMB2FAfYjuIA21EcYDuKA2xHcYDtKA6wHcUBtqM4wHYUB9iO4gDbURxgO4oDbEdxgO0oDrAdxQG2ozjAdhQH2I7iANtRHGA7igNsR3GA7SgOsB3FAbajOMB2FAfYjuIA21EcYDuKA2xHcYDtKA6wHcUBtqM4wHYUB9iO4gDb+X8gnAjHhTN61AAAAABJRU5ErkJggg==" alt="" />

页面的布局比较简单,就是一个input输入框和一个下拉的ul列表:

<body>
<input type="text" id="q" />
<ul id="ul1"></ul>
</body>

给两个框加上简单的样式,ul的display:none;后续在js里面创建元素:

<style>
#q {width: 300px; height: 30px; padding: 5px; border:1px solid #f90; font-size: 16px;}
#ul1 {border:1px solid #f90; width: 310px; margin: 0;padding: 0; display: none;}
li a { line-height: 30px; padding: 5px; text-decoration: none; color: black; display: block;}
li a:hover{ background: #f90; color: white; }
</style>

js中用到Ajax跨域解决方案JSONP:在页面创建一个script标签,设置src属性,加入参数,用函数angel(data)处理获取过来的数据,将数据加入到ul列表中,点击下拉列表中的数据跳转到百度搜索页面:

<script>
function angel(data) { var oUl = document.getElementById('ul1');
var html = '';
if (data.s.length) {
oUl.style.display = 'block';
for (var i=0; i<data.s.length; i++) {
html += '<li><a target="_blank" href="http://www.baidu.com/s?wd='+data.s[i]+'">'+ data.s[i] +'</a></li>';
}
oUl.innerHTML = html;
} else {
oUl.style.display = 'none';
} }
window.onload = function() { var oQ = document.getElementById('q');
var oUl = document.getElementById('ul1'); oQ.onkeyup = function() { if ( this.value != '' ) {
var oScript = document.createElement('script');
oScript.src = 'http://suggestion.baidu.com/su?wd='+this.value+'&cb=angel';
document.body.appendChild(oScript);
} else {
oUl.style.display = 'none';
}
}
}
</script>

Ajax跨域实现淘宝/百度搜索下拉提示效果的更多相关文章

  1. Ajax跨域:Jsonp实例--百度搜索框下拉提示

    Ajax跨域:Jsonp实例--百度搜索框下拉提示 一.总结 一句话总结:a.找好接口:b.用script标签的src引入文件(json数据):c.定义及实现上一步引入文件中的函数 1.如何找到一个网 ...

  2. vue实现百度搜索下拉提示功能

    这段代码用到vuejs和vue-resouece.实现对接智能提示接口,并通过上下键选择提示项,按enter进行搜索 <!DOCTYPE html> <html lang=&quot ...

  3. jsonp跨域获取数据实现百度搜索

    本菜鸡最近在写某个页面请求数据时,报了如下的错误. Failed to load https://...:No 'Access-Control-Allow-Origin' header is pres ...

  4. jquery 仿百度搜索下拉框的插件

    转载地址:http://www.open-open.com/lib/view/open1420624048437.html 今天写了个下拉插件分享出来 效果: , 可以搜素,也可以使用上下键选择匹配出 ...

  5. 利用 lucene&period;net 实现高效率的 WildcardQuery ,记一次类似百度搜索下拉关键字联想功能的实现。

    打开百度输入  站内搜索也要实现类似功能.最基础的做法,写个方法查数据库搜索历史综合表keywordSearch(先将被搜索过的关键字记录到一张表,记录好他们被搜索的次数.上次搜索的有多少结果) 大概 ...

  6. jquery php 百度搜索框智能提示效果

    这个程序是利用php+ajax+jquery 实现的一个仿baidu智能提示的效果,有须要的朋友能够下载測试哦. 代码例如以下 index.html文件,保保存成index.htm <!DOCT ...

  7. z-blog博客组插件openSug&period;js百度搜索下拉框提示代码

      z-blog安装openSug插件即可获得带有“搜索框提示”功能的搜索框,让z-blog搜索更便捷! https://www.opensug.org/.../opensug_z-blog_v1.0 ...

  8. js中实现输入框类似百度搜索的智能提示效果

    说明:我这里显示的数据采用词典(词典在js中自定义的,看下面文字),主要显示key. 页面元素: <style type="text/css">.search { le ...

  9. JavaScript--模拟百度搜索下拉li

    上效果: 主要思路: 函数indexOf() .join().innerHTML的使用,还有 用完的数组要清空 <!DOCTYPE html> <html> <head ...

随机推荐

  1. 【转】Struts1&period;x系列教程(7):Logic标签库

    转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/archive/2009/01/archive/2009/0 ...

  2. Android Launcher分析和修改13——实现Launcher编辑模式&lpar;1&rpar; 壁纸更换

    已经很久没更新Launcher系列文章,今天不分析源码,讲讲如何在Launcher里面添加桌面设置的功能.目前很多第三方Launcher或者定制Rom都有简单易用的桌面设置功能.例如小米MIUI的La ...

  3. jQuery Mobile&lowbar;表单元素

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. plsql通过instantclient连接oracle数据库报连接超时

    配置:数据库oracle10.2;服务器操作系统centos5.5:客户机操作系统win7 32位:plsql版本10.0.5 配置前提必须关闭客户机与服务器操作系统的防火墙,否则会出现“连接超时”的 ...

  5. C&num;:获取时间年月日时分秒格式

    //获取日期+时间 DateTime.Now.ToString();            // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToStri ...

  6. U31网管配置

    1.新建网元和子架配置: 在拓扑图空白处新建对象-创建承载传输网元-选择设备-填写网元名称(A).网元类型.IP(查询出SNP的),网关IP会自动在此基础上加2.如果数据库为空先选离线- 机架子架配置 ...

  7. Python中字典dict

    dict字典 字典是一种组合数据,没有顺序的组合数据,数据以键值对形式出现 # 字典的创建 # 创建空字典1 d = {} print(d) # 创建空字典2 d = dict() print(d) ...

  8. 【laravel5&period;6】 laravel中间件内生成参数并且传递到控制器的2种方法

    中间件方法: /** * 自定义中间件: * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return ...

  9. 【代码审计】Cscms&lowbar;v4&period;1 任意文件删除漏洞实例

    环境搭建: CSCMS :http://www.chshcms.com/ 网站源码版本:Cscms_v4.1正式版(发布日期:2017-06-05) 程序源码下载:https://github.com ...

  10. eclipse上一次没有正确关闭,导致启动的时候卡死错误解决方法

    关于 eclipse启动卡死的问题(eclipse上一次没有正确关闭,导致启动的时候卡死错误解决方法),自己常用的解决方法: 方案一(推荐使用,如果没有这个文件,就使用方案二): 到<works ...