仿百度联想词列表

第一步:将需要联想的词在页面加载的时候就加载到页面

例如:放到隐藏于中

<div style="display:none;">					
								<ul class='dropL' id="ul_level1Tag_json"></ul>
								<ul class='dropL' id="ul_level2Tag_json"></ul>
								<ul class='dropL' id="ul_level3Tag_json"></ul>
								<ul class='dropL' id="ul_level4Tag_json"></ul>
							</div>

function initLabels(url, id) {
	$.get(url , function(data){
		var total = data.service.totalRecord;
		var result = data.service.result;
		var $ul = $('#ul_'+id);
		for(var i=0; i<total; i++) {						
			var record = result[i];
			var ope =  "<li id='"+record.id+"'>"+record.name+"</li>";
			$(ope).appendTo($ul);
		}
	});
}


第二步骤:需要联想的框加样式

<tr><td>一级标签</td><td><input name="level1Tag_json" class="needCheck" value="${sdArticle.level1Tag_json}" style="width:300px;"/></td></tr>
								<tr><td>二级标签</td><td><input name="level2Tag_json" class="needCheck" value="${sdArticle.level2Tag_json}" style="width:300px;"/></td></tr>

第三步:重写方法,并通过样式关联到想加载联想词的输入框

(function($){
	$.fn.assoc = function(){
		var index = -1;
		var _val = "";
		$(this).live('keyup',function(e){
			var $this = $(this);	
			var name = $(e.target).attr('name');	
			$this.parent().addClass('pos-r');
			$this.css({'display':'block'});
			var $html = $("#ul_"+name).html();
			//var lis = '<li>11dddd</li><li>22dddd</li><li>33dddd</li><li>44dddd</li>';
			//document.write($html+"====="+lis);			
			var $ul = $("<ul class='dropL'>"+$html+"</ul>");			
			var $d = $this.next('ul.dropL');
			$d.length=="0"?$ul.insertAfter($this).show():$d.show();
			$d = $this.next('ul.dropL');
			$d.css({width:$this.outerWidth()});
			
			$('ul.dropL li').live('mouseover',function(){
				this.style.background='#eee';
			}).live('mouseout',function(){
				this.style.background='';
			}).live('click',function(e){
				e= e||window.event;
				e.stopPropagation();
				$(this).parent().prev().val($(this).text());	
				$d.hide();
			})
			
			//键盘选值
			e=e||window.event;
			var _key = e.keyCode,_n = $d.find('li').length;
			if(_key!=40&&_key!=38&&_key!=37&&_key!=39){
				_val = $this.val();
				
				//$d.html('');//
				$d.children('li').each(function(){
					$(this).show();
				})
				$d.children('li').each(function(){
					var liHtml = $(this).html();
					var content = $(this).attr('content');				
					if(content.indexOf(_val) == -1 && liHtml.indexOf(_val) == -1) {
						$(this).hide();
					}
				});			
			};
			if(_key==40){//向下
				if(index >= _n){
					index=-1;				
				}
				index++;				
				while($d.find('li').eq(index).is(":hidden")) {
					index++;
				}
				$d.find('li').css('background','#fff')
				$d.find('li').eq(index).css('background','#eee');
				$this.val($d.find('li').eq(index).text());
				if(index==_n){
					$this.val(_val);
				}
			}else if(_key==38){//向上
				if(index < 0){
					index=_n;
					$this.val(_val);
				}else if(index==0){
					index=_n+1;
					$this.val(_val);
				}
				index--;
				while($d.find('li').eq(index).is(":hidden")) {
					index--;
				}
				$d.find('li').css('background','#fff');
				$d.find('li').eq(index).css('background','#eee');				
				$this.val($d.find('li').eq(index).text());
				if(index==_n){
					$this.val(_val);
				}
			}else if(_key==13){
				$('ul.dropL').hide();
			}
		});	
		$(document).bind('click',function(){
			$('ul.dropL').hide();
		});	
	}
})(jQuery);
$('.needCheck').assoc();



静态页面DEMO例子:

<!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=gbk" />
<title>仿百度联想词列表</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
(function($){
	$.fn.assoc = function(){
		var index = -1;
		var _val = "";
		$(this).live('keyup',function(e){
			var $this = $(this);		
			$this.parent().addClass('pos-r');
			$this.css({'display':'block'});
			var $ul = $("<ul class='dropL'><li>photoshop1</li><li>photoshop1</li><li>photoshop1</li><li>photoshop21</li><li>photoshop22</li><li>photoshop23</li><li>photoshop3</li></ul>");
			var $d = $this.next('ul.dropL');
			$d.length=="0"?$ul.insertAfter($this).show():$d.show();
			$d = $this.next('ul.dropL');
			$d.css({width:$this.outerWidth()});
			
			$('ul.dropL li').live('mouseover',function(){
				this.style.background='#eee';
			}).live('mouseout',function(){
				this.style.background='';
			}).live('click',function(e){
				e= e||window.event;
				e.stopPropagation();
				$(this).parent().prev().val($(this).text());	
				$d.hide();
			})
			
			//键盘选值
			e=e||window.event;
			var _key = e.keyCode,_n = $d.find('li').length;
			if(_key!=40&&_key!=38&&_key!=37&&_key!=39){
				_val = $this.val();
				
				//$d.html('');//
				$d.children('li').each(function(){
					$(this).show();
				})
				$d.children('li').each(function(){
					var liHtml = $(this).html();					
					if(liHtml.indexOf(_val) == -1) {
						$(this).hide();
						//$d.hide();
					}
					//else {
					//	$d.append($(this));
					//}
				});			
			};
			if(_key==40){//向下
				if(index >= _n){
					index=-1;				
				}
				index++;
				$d.find('li').css('background','#fff')
				$d.find('li').eq(index).css('background','#eee');
				while($d.find('li').eq(index).is(":hidden")) {
					index++;
				}
				$this.val($d.find('li').eq(index).text());
				if(index==_n){
					$this.val(_val);
				}
			}else if(_key==38){//向上
				if(index < 0){
					index=_n;
					$this.val(_val);
				}else if(index==0){
					index=_n+1;
					$this.val(_val);
				}
				index--;
				$d.find('li').css('background','#fff');
				$d.find('li').eq(index).css('background','#eee');
				$this.val($d.find('li').eq(index).text());
				if(index==_n){
					$this.val(_val);
				}
			}else if(_key==13){
				$('ul.dropL').hide();
			}
		});	
		$(document).bind('click',function(){
			$('ul.dropL').hide();
		});	
	}
})(jQuery);

</script>
<script>
$('.needCheck').assoc();
</script>
<style>
*{ margin:0; padding:0; list-style:none; font-size:12px;}
.dropL{ border:1px solid #ccc; position:absolute; display:none; background:#fff; z-index:999}
.dropL li{ line-height:25px; height:25px; background:#fff; padding:0 5px; display:block; color:#666; cursor:pointer}
</style>
</head>

<body>

<input placeholder="输入你的专长技能" class="needCheck" type="text"><br />
<br />
<input placeholder="输入你的专长技能" class="needCheck" type="text">
</body>
</html>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
插件使用配置(config)还是很灵活的,当然也是弊端,配置不当,效果就没了。 使用时候,重要的参数如下(此demo参数配置点击右键看源码): url: 'http://suggest.taobao.com/sug?code=utf-8&extras=1', queryName: 'q', //url?queryName=value,默认为输入框的name属性 jsonp: 'callback', //设置此参数名,将开启jsonp跨域功能(我要调淘宝数据,非跨域不可,淘宝的回调参数名就是callback),否则使用json数据结构 item: 'li', //下拉提示项目单位的选择器,默认一个li是一条提示,与processData写法相关。 processData: function(data){ }//自定义处理返回的数据,该方法可以return一个html字符串或jquery对象,将被写入到提示的下拉层中。 右键查看源码,将看到本demo所编写的processData函数是怎样的,所以这个参数是把如何表现交给你来做了,但别忘了配合 item 参数 getCurrItemValue: function($currItem){ }//定义如何去取得当前提示项目的值并返回值,插件根据此函数获取当前提示项目的值,并填入input中,此方法应根据processData参数来定义。 是的,如果你自定义了processData,这个参数恐怕也需要自定义,默认是获取$currItem.html(),你也可以return $currItem.attr('an attrName'); 右键查看源码,将看到本demo所编写的getCurrItemValue函数是怎样的 textchange: function($input){}, //不同于change事件在失去焦点触发,inchange依赖本插件,只要内容有变化,就会触发,并传入input对象 onselect: function($currItem){} //当选择了下拉的当前项目时执行,并传入当前项目。比如选择了某个提示项目,就提交表单。 sequential: 0, //按着方向键不动是否可以持续选择,默认不可以,设置值可以是任何等价的boolean

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值