JS實(shí)現(xiàn)仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)方法
本文實(shí)例講述了JS實(shí)現(xiàn)仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)</title>
<style type="text/css" media="screen">
body
{
font: 11px arial;
}
.suggest_link
{
width:120px;
background-color: #FFFFFF;
padding: 2px 6px 2px 6px;
}
.suggest_link_over
{
width:120px;
background-color: #E8F2FE;
padding: 2px 6px 2px 6px;
}
#suggestResult
{
position: absolute;
background-color: #FFFFFF;
text-align: left;
border: 1px solid #000000;
}
/*input*/
.input_on
{
padding: 2px 8px 0pt 3px;
height: 18px;
border: 1px solid #999;
background-color: #FFFFCC;
}
.input_off
{
padding: 2px 8px 0pt 3px;
height: 18px;
border: 1px solid #CCC;
background-color: #FFF;
}
.input_move
{
padding: 2px 8px 0pt 3px;
height: 18px;
border: 1px solid #999;
background-color: #FFFFCC;
}
.input_out
{
/*height:16px;默認(rèn)高度*/
padding: 2px 8px 0pt 3px;
height: 18px;
border: 1px solid #CCC;
background-color: #FFF;
}
</style>
<script language="javascript" type="text/javascript">
var $ = document.getElementById;
//創(chuàng)建XMLHttpRequest對(duì)象
function createXMLHttpRequest() {
var obj;
if (window.XMLHttpRequest) { //Mozilla 瀏覽器
obj = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE瀏覽器
try {
obj = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
obj = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { }
}
}
return obj;
}
//當(dāng)輸入框的內(nèi)容變化時(shí),調(diào)用該函數(shù)
function searchSuggest() {
var inputField = $("txtSearch");
var suggestText = $("suggestResult");
if (inputField.value.length > 0) {
var o = createXMLHttpRequest();
var url = "SearchResult.ashx?searchText=" + escape(inputField.value);
o.open("GET", url, true);
o.onreadystatechange = function () {
if (o.readyState == 4) {
if (o.status == 200) {
var sourceItems = o.responseText.split("\n");
if (sourceItems.length > 1) {
suggestText.style.display = "";
suggestText.innerHTML = "";
for (var i = 0; i < sourceItems.length - 1; i++) {
var sourceText = sourceItems[i].split("@")[1];
var sourceValue = sourceItems[i].split("@")[0];
var s = "<div onmouseover=\"javascript:suggestOver(this);\" ";
s += " onmouseout=\"javascript:suggestOut(this);\" ";
s += " onclick=\"javascript:setSearch('" + sourceText + "','" + sourceValue + "');\" ";
s += " class=\"suggest_link\" >" + sourceText + "</div>";
suggestText.innerHTML += s;
}
}
else {
suggestText.style.display = "none";
}
}
}
}; //指定響應(yīng)函數(shù)
o.send(null); // 發(fā)送請(qǐng)求
}
else {
suggestText.style.display = "none";
}
}
function delayExecute() {
$("valueResult").value = "";
window.setTimeout(function () { searchSuggest() }, 800);
//延時(shí)處理
}
function suggestOver(div_value) {
div_value.className = "suggest_link_over";
}
function suggestOut(div_value) {
div_value.className = "suggest_link";
}
function setSearch(a, b) {
$("txtSearch").value = a;
$("valueResult").value = b;
var div = $("suggestResult");
div.innerHTML = "";
div.style.display = "none";
}
function showResult() {
alert($("txtSearch").value + $("valueResult").value);
}
</script>
</head>
<body>
<form id="form1" action="">
<input type="text" id="txtSearch" name="txtSearch" onkeyup="delayExecute();" size="20"
class="input_out" onfocus="this.className='input_on';this.onmouseout=''"
onblur="this.className='input_off';this.onmouseout=function(){this.className='input_out'};"
onmousemove="this.className='input_move'" onmouseout="this.className='input_out'" />
<input type="hidden" id="valueResult" name="valueResult" value="" />
<br />
<div id="suggestResult" style="display: none">
</div>
<br/>
<input id="button1" type="button" value="提交" onclick="showResult();" />
</form>
</body>
</html>
服務(wù)器端C#代碼
<%@ WebHandler Language="C#" Class="SearchResult" %>
using System;
using System.Web;
using System.Data;
public class SearchResult : IHttpHandler {
public void ProcessRequest (HttpContext context) {
object QueryWord=context.Request.QueryString["searchText"];
if (QueryWord != null)
{
if (QueryWord.ToString().Trim().Length > 0)
{
DataTable dt = getDB();
string returnText = "";
if (dt != null && dt.Rows.Count > 0)
{
DataRow[] dr = dt.Select(" name like '%" + QueryWord .ToString()+ "%' ");
if (dr.Length > 0)
{
for (int i = 0; i < dr.Length; i++)
{
//可設(shè)置返回多字符串
returnText += dr[i]["id"].ToString() + "@" + dr[i]["name"].ToString() + "\n";
}
}
}
context.Response.Write(returnText);
context.Response.End();
}
}
}
public bool IsReusable {
get {
return false;
}
}
/// <summary>
/// 獲取數(shù)據(jù)源的方法
/// </summary>
/// <returns>數(shù)據(jù)源</returns>
private DataTable getDB()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("age");
dt.Rows.Add(new object[] { "000001", "張三", "26" });
dt.Rows.Add(new object[] { "000002", "張曉", "26" });
dt.Rows.Add(new object[] { "000003", "張嵐", "27" });
dt.Rows.Add(new object[] { "000004", "李四", "25" });
dt.Rows.Add(new object[] { "000005", "李星", "27" });
return dt;
}
}
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- js實(shí)現(xiàn)搜索框關(guān)鍵字智能匹配代碼
- 基于Vue.js實(shí)現(xiàn)簡(jiǎn)單搜索框
- JavaScript實(shí)現(xiàn)搜索框的自動(dòng)完成功能(一)
- JavaScript實(shí)現(xiàn)百度搜索框效果
- JS+Ajax實(shí)現(xiàn)百度智能搜索框
- 自動(dòng)完成的搜索框javascript實(shí)現(xiàn)
- JS實(shí)現(xiàn)京東首頁(yè)之頁(yè)面頂部、Logo和搜索框功能
- JS實(shí)現(xiàn)微信彈出搜索框 多條件查詢功能
- javascript搜索框效果實(shí)現(xiàn)方法
- JavaScript仿京東搜索框?qū)嵗?/a>
相關(guān)文章
基于原生js運(yùn)動(dòng)方式關(guān)鍵點(diǎn)的總結(jié)(推薦)
下面小編就為大家?guī)?lái)一篇基于原生js運(yùn)動(dòng)方式關(guān)鍵點(diǎn)的總結(jié)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
解決Layui當(dāng)中的導(dǎo)航條動(dòng)態(tài)添加后渲染失敗的問(wèn)題
今天小編就為大家分享一篇解決Layui當(dāng)中的導(dǎo)航條動(dòng)態(tài)添加后渲染失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
JS實(shí)現(xiàn)調(diào)用本地?cái)z像頭功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)調(diào)用本地?cái)z像頭功能,結(jié)合實(shí)例形式分析了Javascript基于瀏覽器對(duì)本地硬件操作簡(jiǎn)單實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-05-05
js一般方法改寫(xiě)成面向?qū)ο蠓椒ǖ臒o(wú)限級(jí)折疊菜單示例代碼
本例是應(yīng)用別人的例子,原來(lái)那位老兄是用一般方法寫(xiě)成的無(wú)限級(jí)折疊菜單,通過(guò)了一些簡(jiǎn)化修改,將原來(lái)的例子改成了面向?qū)ο蟮姆绞?/div> 2013-07-07
前端項(xiàng)目打包部署后如何避免讓用戶強(qiáng)制去清除瀏覽器緩存
這篇文章主要介紹了前端項(xiàng)目打包部署后如何避免讓用戶強(qiáng)制去清除瀏覽器緩存的相關(guān)資料,文中講解了瀏覽器緩存機(jī)制及其對(duì)性能優(yōu)化的重要性,探討了如何通過(guò)設(shè)置Cache-Control頭部、添加資源版本號(hào)或利用Webpack的文件命名特性來(lái)控制緩存,需要的朋友可以參考下2024-12-12最新評(píng)論

