Ajax實(shí)現(xiàn)搜索框提示功能_第1頁
Ajax實(shí)現(xiàn)搜索框提示功能_第2頁
Ajax實(shí)現(xiàn)搜索框提示功能_第3頁
Ajax實(shí)現(xiàn)搜索框提示功能_第4頁
Ajax實(shí)現(xiàn)搜索框提示功能_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第Ajax實(shí)現(xiàn)搜索框提示功能title搜索框輸入文字自動(dòng)提示/title

linkrel="stylesheet"href="./assets/bootstrap/dist/css/bootstrap.min.css"rel="externalnofollow"/

styletype='text/css'

.list-group{

display:none;

.container{

padding-top:150px;

/style

/head

body

div

div

inputtype="text"placeholder="請輸入搜索的關(guān)鍵字"id="search"/

ulid="list-box"

!--li提示文顯示的地方/li--

/ul

/div

/div

scriptsrc="./js/ajax.js"/script

scriptsrc="./js/template-web.js"/script

scripttype="text/html"id="tpl"

{{eachresult}}

li{{$value}}/li

{{/each}}

/script

script

//獲取搜索框

varsearchInput=document.getElementById('search');

//獲取提示文字的存放問起

varlistBox=document.getElementById('list-box');

//存儲定時(shí)器的變量

vartimer=null;

//輸入框輸入觸發(fā)事件,并清除防抖/節(jié)流

searchInput.oninput=function(){

//清除上一次開啟的定時(shí)器

clearTimeout(timer);

//獲取用戶輸入的內(nèi)容

varkey=this.value;

//如果沒有在搜索框輸入內(nèi)容

if(key.trim().length==0){

//將提示下拉信息隱藏

listBox.style.display='none';

//阻止程式向下執(zhí)行

return;

//開啟定時(shí)器,讓請求延遲發(fā)送

timer=setTimeout(

function(){

//向服務(wù)器發(fā)送請求

ajax({

type:'get',

url:'http://localhost:3000/searchAutoPrompt',

data:{

key:key

success:function(result){

console.log(result);

//使用模板引擎拼接字符串

varhtml=template('tpl',{result:result});//獲取的result為數(shù)組

//拼接好的字符串顯示在頁面

listBox.innerHTML=html;

console.log(html);

//顯示ul容器

listBox.style.display='block';

},800);

/script

/body

/html

Ajax封裝方法:

functionajax(options){

//定義默認(rèn)

vardefaults={

type:'get',

url:'',

data:{},

Headers:{'Content-Type':'application/x-www-form-urlencoded'},

success:function(){},

error:function(){}

//用options中的對象覆蓋defaults中對象

Object.assign(defaults,options);

//創(chuàng)建

varxhr=newXMLHttpRequest();

//拼接請求參數(shù)變量

varparams='';

//循環(huán)用戶傳遞進(jìn)來的對象格式參數(shù)

for(varattrindefaults.data){

//將參數(shù)轉(zhuǎn)換成字符串格式

params+=attr+'='+defaults.data[attr]+'

//字符串截取,將最后的截取掉

params=params.substr(0,params.length-1);

//判斷請求方式

if(defaults.type=='get'){

defaults.url=defaults.url+''+params;

//配置

xhr.open(defaults.type,defaults.url);

//發(fā)送請求

if(defaults.type=='post'){

//用戶期望的向服務(wù)端傳遞的請求參數(shù)類型

varcontentType=defaults.Headers['Content-Type'];

//post請求時(shí)必須設(shè)置的

xhr.setRequestHeader('Content-Type',contentType);

//判斷請求參數(shù)類型

if(contentType=='application/json'){

xhr.send(JSON.stringify(defaults.data));

}else{

xhr.send(JSON.stringify(params));

}else{

xhr.send();

//監(jiān)聽onload事件,當(dāng)接收完響應(yīng)數(shù)據(jù)后觸發(fā)

xhr.onload=function(){

//xhr.getResponseHeader()

//獲取響應(yīng)頭部數(shù)據(jù)

varcontentType=xhr.getResponseHeader('Content-Type');

//服務(wù)端返回的數(shù)據(jù)

varresponseText=xhr.responseText;

if(contentType.includes('application/json')){

//把JSON字符串轉(zhuǎn)換為JSON對象

responseText=JSON.parse(responseText);

//對http狀態(tài)碼判斷,判斷是否等于200

if(xhr.status==200){

//調(diào)用處理成功情況的函數(shù)

defaults.success(responseText,xhr);

}else{

//調(diào)用處理失敗的情況函數(shù)

defaults.error(responseText,xhr);

}

服務(wù)器端測試代碼:

/****server.js****/

//一個(gè)簡單的后端路由

//1.引入express框架

constexpress=require('express');

constfs=require('fs');

//2.引入路徑處理模塊

constpath=require('path');

constbodyParser=require('body-parser');//post

//3.創(chuàng)建web服務(wù)器

constapp=express();

//post

//extended:返回的對象是一個(gè)鍵值對,當(dāng)extended為false的時(shí)候,鍵值對中的值就為'String'或'Array'形式,為true的時(shí)候,則可為任何數(shù)據(jù)類

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論