仿百度搜索框效果

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery百度搜索提示框效果代码</title>

<link rel="stylesheet" href="css/jquery.bigautocomplete.css" type="text/css" />

<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/baiduSearch.js"></script>
<script type="text/javascript">
$(function(){

	$("#tt").bigAutocomplete({
		width:543,
		data:[{title:"中国好声音",result:{ff:"qq"}},
		{title:"中国移动网上营业厅"},
		{title:"中国银行"},
		{title:"中国移动"},
		{title:"中国好声音第三期"},
		{title:"中国好声音 第一期"},
		{title:"中国电信网上营业厅"},
		{title:"中国工商银行"},
		{title:"中国好声音第二期"},
		{title:"中国地图"}],
		callback:function(data){
			alert(data.title);
		}
	});

})
</script>

</head>
<body>

<style type="text/css">
.text{
	width:529px;
	height:22px;
	padding:4px 7px;
	padding:6px 7px 2px\9;
	font:16px arial;
	border:1px solid #cdcdcd;
	border-color:#9a9a9a #cdcdcd #cdcdcd #9a9a9a;
	vertical-align:top;
	outline:none;
	margin:0 5px 0 0;
}
</style>

<div class="demo">
	<input type="text" id="tt" value="" class="text" />
</div>

<script>
	window.onresize = function(){
		console.log("change")
	}

</script>
</body>
</html>

js/baiduSearch.js
(function($) {
	var bigAutocomplete = new function() {
		this.currentInputText = null;
		this.functionalKeyArray = [9,20,13,16,17,18,91,92,93,45,36,33,34,35,37,39,112,113,114,115,116,117,118,119,120,121,122,123,144,19,145,40,38,27];
		this.holdText = null

		this.init = function() {
			$("body").append("<div id='bigAutocompleteContent' class='bigautocomplete-layout'></div>");
			$(document).bind('mousedown',function(event){
				var $target = $(event.target);
				if((!($target.parents().andSelf().is('#bigAutocompleteContent'))) && (!$target.is(bigAutocomplete.currentInputText))){
					bigAutocomplete.hideAutocomplete();
				}
			})
			
			$("#bigAutocompleteContent").delegate("tr", "mouseover", function() {
				$("#bigAutocompleteContent tr").removeClass("ct");
				$(this).addClass("ct");
			}).delegate("tr", "mouseout", function() {
				$("#bigAutocompleteContent tr").removeClass("ct");
			});		
			
			$("#bigAutocompleteContent").delegate("tr", "click", function() {
				bigAutocomplete.currentInputText.val($(this).find("div:last").html());
				var callback_ = bigAutocomplete.currentInputText.data("config").callback;
				if($("#bigAutocompleteContent").css("display") != "none" && callback_ && $.isFunction(callback_)){
					callback_($(this).data("jsonData"));
				}
				bigAutocomplete.hideAutocomplete();
			})
		}

		this.autocomplete = function(param) {
			if($("body").length > 0 && $("#bigAutocompleteContent").length <= 0){
				bigAutocomplete.init();
			}

			var $this = $(this)
			var $bigAutocompleteContent = $("#bigAutocompleteContent")
			this.config = {width:$this.outerWidth() - 2,
			               url:null,
			               data:null,
			               callback:null}
			$.extend(this.config,param)

			$this.data("config", this.config)

			$this.keydown(function(event) {
				switch(event.keyCode) {
				case 40: // 向下
					if($bigAutocompleteContent.css("display") == "none")return;
					
					var $nextSiblingTr = $bigAutocompleteContent.find(".ct");
					if($nextSiblingTr.length <= 0){
						$nextSiblingTr = $bigAutocompleteContent.find("tr:first");
					}else{
						$nextSiblingTr = $nextSiblingTr.next();
					}
					$bigAutocompleteContent.find("tr").removeClass("ct");
					
					if($nextSiblingTr.length > 0){
						$nextSiblingTr.addClass("ct");
						$this.val($nextSiblingTr.find("div:last").html());
						
						$bigAutocompleteContent.scrollTop($nextSiblingTr[0].offsetTop - $bigAutocompleteContent.height() + $nextSiblingTr.height() );
						
					}else{
						$this.val(bigAutocomplete.holdText);
					}
					break
				case 38: // 向上
					if($bigAutocompleteContent.css("display") == "none")return;
					
					var $previousSiblingTr = $bigAutocompleteContent.find(".ct");
					if($previousSiblingTr.length <= 0){
						$previousSiblingTr = $bigAutocompleteContent.find("tr:last");
					}else{
						$previousSiblingTr = $previousSiblingTr.prev();
					}
					$bigAutocompleteContent.find("tr").removeClass("ct");
					
					if($previousSiblingTr.length > 0){
						$previousSiblingTr.addClass("ct");
						$this.val($previousSiblingTr.find("div:last").html());
						
						$bigAutocompleteContent.scrollTop($previousSiblingTr[0].offsetTop - $bigAutocompleteContent.height() + $previousSiblingTr.height());
					}else{
						$this.val(bigAutocomplete.holdText);
					}
					break;
				case 27: // ESC
					bigAutocomplete.hideAutocomplete();
					break;
				}
			})

			$this.keyup(function(event) {
				var k = event.keyCode;
				var ctrl = event.ctrlKey;
				var isFunctionalKey = false;
				for(var i=0;i<bigAutocomplete.functionalKeyArray.length;i++){
					if(k == bigAutocomplete.functionalKeyArray[i]){
						isFunctionalKey = true;
						break;
					}
				}
				//不是功能键同时 ctrl被按下及c与x键
				if(!isFunctionalKey && (!ctrl || (ctrl && k == 67) || (ctrl && k == 88)) ){
					var config = $this.data("config");
					
					var offset = $this.offset();
					$bigAutocompleteContent.width(config.width);
					var h = $this.outerHeight() - 1;
					$bigAutocompleteContent.css({"top":offset.top + h,"left":offset.left});
					
					var data = config.data;
					var url = config.url;
					var keyword_ = $.trim($this.val());
					if(keyword_ == null || keyword_ == ""){
						bigAutocomplete.hideAutocomplete();
						return;
					}					
					if(data != null && $.isArray(data) ){
						var data_ = new Array();
						for(var i=0;i<data.length;i++){
							if(data[i].title.indexOf(keyword_) > -1){
								data_.push(data[i]);
							}
						}
						
						makeContAndShow(data_);
					}else if(url != null && url != ""){
						$.post(url,{keyword:keyword_},function(result){
							makeContAndShow(result.data)
						},"json")
					}

					
					bigAutocomplete.holdText = $this.val();
				}
				//回车
				if(k == 13){
					var callback_ = $this.data("config").callback;
					if($bigAutocompleteContent.css("display") != "none"){
						if(callback_ && $.isFunction(callback_)){
							callback_($bigAutocompleteContent.find(".ct").data("jsonData"));
						}
						$bigAutocompleteContent.hide();						
					}
				}
			})

			// 显示下拉列表及数据
			function makeContAndShow(data_){
				if(data_ == null || data_.length <=0 ){
					return;
				}
				
				var cont = "<table><tbody>";
				for(var i=0;i<data_.length;i++){
					cont += "<tr><td><div>" + data_[i].title + "</div></td></tr>"
				}
				cont += "</tbody></table>";
				$bigAutocompleteContent.html(cont);
				$bigAutocompleteContent.show();
				
				$bigAutocompleteContent.find("tr").each(function(index){
					$(this).data("jsonData",data_[index]);
				})
			}			
					
			$this.focus(function(){
				bigAutocomplete.currentInputText = $this;
			})
		}

		// 隐藏下拉列表
		this.hideAutocomplete = function() {
			var $bigAutocompleteContent = $("#bigAutocompleteContent");
			if($bigAutocompleteContent.css("display") != "none"){
				$bigAutocompleteContent.find("tr").removeClass("ct");
				$bigAutocompleteContent.hide();
			}
		}
	}

	$.fn.bigAutocomplete = bigAutocomplete.autocomplete
})(jQuery)


css/jquery.bigautocomplete.css
@charset "utf-8";
.bigautocomplete-layout{display:none;background-color:#FFFFFF;border:1px solid #BCBCBC;position:absolute;z-index:100;max-height:220px;overflow-x:hidden;overflow-y:auto; }
.bigautocomplete-layout table{border-collapse:collapse;border-spacing:0;background:none repeat scroll 0 0 #FFFFFF;width:100%;cursor:default;}
.bigautocomplete-layout table tr{background:none repeat scroll 0 0 #FFFFFF;}
.bigautocomplete-layout .ct{background:none repeat scroll 0 0 #D2DEE8 !important;}
.bigautocomplete-layout div{word-wrap:break-word;word-break:break-all;padding:1px 5px;}






        
百度搜索框不能输入解决方法 有的电脑百度输入框不能输入,还有其他的网页上的输入框都不可以输入。用这个东东就是了。 立即下载

相关推荐

仿百度新闻热搜词效果

仿百度新闻热搜词效果

【JavaScript】让select选择框可输入和可下拉选择:

【JavaScript】让select选择框可输入和可下拉选择:

SunPeng的博客 5979

仿百度搜索框提示效果+源代码

仿百度搜索框提示效果+源代码仿百度搜索框提示效果+源代码仿百度搜索框提示效果+源代码

百度搜索框提示功能简单实现

搜索框中输入的时候会有百度的只能提示。 代码也很简单,整个html只有24行,关键的代码不超过10行。

简单就是美 1万+

wasd和上下左右互换了怎么办?

刚买了一个机械键盘,用的时候发现wasd和上下左右互换了,现在把我的解决方案写在下边 供大家参考: 其实wasd和上下左右互换了是很多游戏键盘特有的功能,wasd和上下左右互换了并不是键盘的硬件故障, 只要在键盘上同时按下FN+Windows键就可以完成wasd和上下左右互换了, 转载于:https://www.cnblogs.com/Jack1816274408/p/...

1万+

百度地图(二) - 模仿百度地图搜索框

百度地图(二) - 模仿百度地图搜索框1 模仿百度地图搜索框 1 在activity_Main在添加 TextView <TextView android:id="@+id/tvSeek" android:layout_marginTop="20dp" android:layout_marginRight="20dp" android

风与蒲公英 4178

html搜索框下拉怎么做,一步一步教你实现仿百度搜索框下拉效果(上)

最终效果(chrome 下):搜索框下拉 demo今天就来简单讲解下如何做这样一个类似百度搜索框的下拉效果。1、html 以及 css 部分首先你得要有个输入框,这里我用了控件,其次当用户在输入框中输入内容后,下拉效果随即出现,比如在我的代码中最多会出现 10 个联想词,那么就得写 10 个 div(来显示这些词),这里我用了 table 元素,这里注意 table 中的 td 元素还得有个...

weixin_42389043的博客 3862

php百度搜索框代码,基于jquery仿百度搜索框效果代码_jquery

先看看整个的效果图:图一:图二:图三:图四:大概的效果图就这样,接下来直接看源码页面:CSS:.autoSearchText{border:solid 1px #CFCFCF;height:20px;color:Gray;}.menu_v{margin:0;padding:0;line-height:20px;font-size:12px;list-style-type:none;}.menu_v...

weixin_34805308的博客 380

一步一步教你实现仿百度搜索框下拉效果

最终效果(chrome 下): 搜索框下拉 demo 今天就来简单讲解下如何做这样一个类似百度搜索框的下拉效果。 1、html 以及 css 部分 首先你得要有个输入框,这里我用了 &lt;input type='text' /&gt; 控件,其次当用户在输入框中输入内容后,下拉效果随即出现,比如在我的代码中最多会出现 10 个联想词,那么就得写 10 个 div(来显示这些词),这里我用了 t...

weixin_34221112的博客 1264

一步一步教你实现仿百度搜索框下拉效果(上)

一步一步教你实现仿百度搜索框下拉效果(上) 最终效果(chrome 下): 搜索框下拉 demo 今天就来简单讲解下如何做这样一个类似百度搜索框的下拉效果。 1、html 以及 css 部分 首先你得要有个输入框,这里我用了 <input type='text' /> 控件,其次当用户在输入框中输入内容后,下拉效果随即出现,比如在我的代码中最多会出现 10 个联想词,那么就

zdy0_2004的专栏 7401

html5里百度搜索框怎么做,基于jquery仿百度搜索框效果代码

先看看整个的效果图:图一:图二:图三:图四:大概的效果图就这样,接下来直接看源码页面:$(document).ready(function() {$('#autoSearchText').autoSearchText({ width: 300, itemHeight: 150,minChar:1, datafn: getData, fn: alertMsg });});function alert...

weixin_42361622的博客 984

仿百度搜索框

仿百度搜索框,自动匹配搜索结果,利用Ajax无刷新操作和Jquery生成页面元素

php layui仿百度搜索,仿百度自动补全搜索框效果

诗涵艺馨 02016/10/19 19:10:44问,这个获取文本框的值是怎么获取的id="o"onkeyup="autoComplete.start(event)"/>//在这里我需要知道你是怎么动态获取文本的值?vardataf=[];$.ajax({url:"../sousuo/sousuo.aspx/comp",type:"POST",dataType:"json",...

weixin_42577243的博客 497

仿百度自动补全搜索框效果(*附有源码下载)

 页面效果及代码,(源码在下面的下载链接) <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="css/semantic.css"> <script type="te...

weixin_30349597的博客 304

仿百度搜索框自动补全智能提示效果

body {     margin-left: 0px;     margin-top: 0px;     margin-right: 0px;     margin-bottom: 0px; } .auto_hidden {     width:204px;border-top: 1px solid #333;     border-bottom: 1px solid #333;

qq_27620757的博客 759

html 搜索框自动完成,仿百度自动补全搜索框效果

诗涵艺馨 02016/10/19 19:10:44问,这个获取文本框的值是怎么获取的id="o"onkeyup="autoComplete.start(event)"/>//在这里我需要知道你是怎么动态获取文本的值?vardataf=[];$.ajax({url:"../sousuo/sousuo.aspx/comp",type:"POST",dataType:"json",...

weixin_36441329的博客 154

php mysql 搜索框提示_php mysql ajax仿百度谷歌搜索下拉自动提示框效果

使用百度google时我们都会发现只要输入一个字就会有相关提示内容了,这个很好的提升了网站的体验了,下面我来与大家一起学习一个php mysql ajax仿百度谷歌搜索下拉自动提示框效果实例。很久以前就写了,现在拿到博客给大家分享一下。仿百度谷歌搜索下拉自动提示原理并不是很复杂,主要就是通过ajax这座桥梁。没有百度那么强大,它可以匹配拼音等,我目前水平确实做不到,我只是实现一下这个效果。我们一起...

weixin_29305337的博客 269

js--搜索框提示仿百度

js--搜索框提示仿百度

281

输入框, 联想搜索,下拉显示搜索结果,JQUERY仿百度搜索框下拉效果

需求:项目页面需要有一个输入框,用户输入部分卡号以后,模糊查询后台数据,返回结果供用户点选。 实现思路:用jQuery,在输入框input 中使用 keyup事件,触发异步,到后台获取相似卡号String,再页面进行拼接ul li,最终用户点击弹出的查询结果,写入input输入框。 代码: 页面部分://记得先引入jQuery &lt;script type="text/javasc...

m0_38093376的博客 3847
上一篇: 得到json 中的key 值
csdn_小飞侠V3
博客等级 码龄15年 1粉丝 10原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值