可编辑的搜索框

初版,优化完我再贴新的,没有做多选框,做的单选
/* 输入框 */ css代码

.newxitongzuoye_main_li .select{
				position: relative;
				border-radius:4px;
				min-width: 320px;;
				border:1px solid rgba(0,0,0,0.15);
				background:rgba(255,255,255,1);
				box-sizing: border-box;
				display: flex;
				display: flex;
				-webkit-box-align: center;
				-ms-flex-align: center;
				align-items: center;
				-ms-flex-wrap: wrap;
				flex-wrap: wrap;
			}
			.my-select-input{
				max-width: 320px;
				height: 28px;	
				border: none;
				outline: none;
				border-radius:4px;
				padding: 4px;
				color: #606266;
				line-height: normal;
				white-space: normal;
				z-index: 1;
			}
			.my_option{
				position: absolute;
				background: #ffffff;
				overflow: hidden;
				min-width: 194px;
				transform-origin: center top;
				z-index: 2181;
				top: 40px;
				border: 1px solid #e4e7ed;
				border-radius: 4px;
				background-color: #fff;
				-webkit-box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
				box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
				margin: 5px ;
				display: none;
			}
			.option_main{
				position: relative;
				overflow: scroll;
			}
			.option_main >dl{
				width: 320px;
				max-height: 160px;
				margin: 0;
				overflow: scroll;
			}
			.option_main >dl dd{
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
				width: 100%;
				padding: 0 20px;
				font-size: 14px;
				color: #606266;
				box-sizing: border-box;
				cursor: pointer;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				height: 32px;
    			line-height: 32px;
			}
			.option_main >dl dd:hover{
				background-color: #f5f7fa;
			}
			.xiaoSpan{
				display: inherit;
				box-sizing: border-box;
				border-color: transparent;
				margin: 2px 0 2px 6px;
				background-color: #f0f2f5;
			}
			.spanText{
				display: inline-block;
				height: 24px;
				padding: 0 8px;
				max-width: 80px;
				text-align: center;
				line-height: 30px;
				overflow: hidden;/*超出部分隐藏*/
				text-overflow:ellipsis;/* 超出部分显示省略号 */
				white-space: nowrap;/*规定段落中的文本不进行换行 */
				/* width: 50px;需要配合宽度来使用f */
			}
			.my-close:hover{
				background: #909399;
			}
			.my-close{
				margin: 0px 5px;
			}
			/* 选中背景色 */
			.selectBGColor{
				background: #F58823;
			}
		
			.my_option2{
				position: absolute;
				background: #ffffff;
				overflow: hidden;
				min-width: 194px;
				transform-origin: center top;
				z-index: 2181;
				top: 40px;
				border: 1px solid #e4e7ed;
				border-radius: 4px;
				background-color: #fff;
				-webkit-box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
				box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
				margin: 5px ;
				display: none;
			}
			.option_main2{
				position: relative;
				overflow: scroll;
			}
			.option_main2 >dl{
				width: 320px;
				max-height: 160px;
				margin: 0;
				overflow: scroll;
			}
			.option_main2 >dl dd{
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
				width: 100%;
				padding: 0 20px;
				font-size: 14px;
				color: #606266;
				box-sizing: border-box;
				cursor: pointer;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				height: 32px;
    			line-height: 32px;
			}
			.option_main2 >dl dd:hover{
				background-color: #f5f7fa;
			}
		</style>/* 输入框 */
			.newxitongzuoye_main_li .select{
				position: relative;
				border-radius:4px;
				min-width: 320px;;
				border:1px solid rgba(0,0,0,0.15);
				background:rgba(255,255,255,1);
				box-sizing: border-box;
				display: flex;
				display: flex;
				-webkit-box-align: center;
				-ms-flex-align: center;
				align-items: center;
				-ms-flex-wrap: wrap;
				flex-wrap: wrap;
			}
			.my-select-input{
				max-width: 320px;
				height: 28px;	
				border: none;
				outline: none;
				border-radius:4px;
				padding: 4px;
				color: #606266;
				line-height: normal;
				white-space: normal;
				z-index: 1;
			}
			.my_option{
				position: absolute;
				background: #ffffff;
				overflow: hidden;
				min-width: 194px;
				transform-origin: center top;
				z-index: 2181;
				top: 40px;
				border: 1px solid #e4e7ed;
				border-radius: 4px;
				background-color: #fff;
				-webkit-box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
				box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
				margin: 5px ;
				display: none;
			}
			.option_main{
				position: relative;
				overflow: scroll;
			}
			.option_main >dl{
				width: 320px;
				max-height: 160px;
				margin: 0;
				overflow: scroll;
			}
			.option_main >dl dd{
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
				width: 100%;
				padding: 0 20px;
				font-size: 14px;
				color: #606266;
				box-sizing: border-box;
				cursor: pointer;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				height: 32px;
    			line-height: 32px;
			}
			.option_main >dl dd:hover{
				background-color: #f5f7fa;
			}
			.xiaoSpan{
				display: inherit;
				box-sizing: border-box;
				border-color: transparent;
				margin: 2px 0 2px 6px;
				background-color: #f0f2f5;
			}
			.spanText{
				display: inline-block;
				height: 24px;
				padding: 0 8px;
				max-width: 80px;
				text-align: center;
				line-height: 30px;
				overflow: hidden;/*超出部分隐藏*/
				text-overflow:ellipsis;/* 超出部分显示省略号 */
				white-space: nowrap;/*规定段落中的文本不进行换行 */
				/* width: 50px;需要配合宽度来使用f */
			}
			.my-close:hover{
				background: #909399;
			}
			.my-close{
				margin: 0px 5px;
			}
			/* 选中背景色 */
			.selectBGColor{
				background: #F58823;
			}
		
			.my_option2{
				position: absolute;
				background: #ffffff;
				overflow: hidden;
				min-width: 194px;
				transform-origin: center top;
				z-index: 2181;
				top: 40px;
				border: 1px solid #e4e7ed;
				border-radius: 4px;
				background-color: #fff;
				-webkit-box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
				box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
				margin: 5px ;
				display: none;
			}
			.option_main2{
				position: relative;
				overflow: scroll;
			}
			.option_main2 >dl{
				width: 320px;
				max-height: 160px;
				margin: 0;
				overflow: scroll;
			}
			.option_main2 >dl dd{
				-webkit-box-sizing: border-box;
				box-sizing: border-box;
				width: 100%;
				padding: 0 20px;
				font-size: 14px;
				color: #606266;
				box-sizing: border-box;
				cursor: pointer;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				height: 32px;
    			line-height: 32px;
			}
			.option_main2 >dl dd:hover{
				background-color: #f5f7fa;
			}
		</style>
	///html代码
<div class="select">
						<!-- <span class="xiaoSpan">
							<span class="spanText">html</span>
							<i class="layui-icon layui-icon-close my-close"></i></span> -->
						<!-- <span class="xiaoSpan"><span class="spanText">javascript</span></span>
						<span class="xiaoSpan"><span class="spanText">css</span></span> -->
						<input type="text" autocomplete="off" class="my-select-input" placeholder="请选择主题">
						<div class="my_option">
							<div class="option_main">
								<dl class="option_list">
								<dd class="list-item">html</dd>
								<dd>javascript</dd>
								<dd>css</dd>
								</dl>
							</div>
							
						</div>
						<div class="my_option2">
								<div class="option_main2">
									<dl class="option_list2">
									<dd class="list-item2"></dd>
									</dl>
								</div>
								
							</div>
					</div>

/ js代码,用的jquery,事件比较近,没有封装,以后有时间改
// 下拉列表
//阻止默认事件,防止鼠标失焦事件宣布中的bug

$('.select').click(function (e){
		e.stopPropagation();
	})
	$('.my-select-input').focus(function(e){//鼠标获取焦点事件
		e.stopPropagation();
		$('.my_option').show();
	})
	// $('.my-select-input').blur(function(){
	//tip :鼠标失焦事件大于其他事件执行
	$(document).click(function (){
		$('.my_option').hide();
	})
	$('.option_list').delegate('dd','click',function(e){ 点击添加节点
		e.stopPropagation()
		$('.select .xiaoSpan').remove();
		$(this).addClass('selectBGColor').siblings().removeClass('selectBGColor'); 
		var str = '<span class="xiaoSpan"><span class="spanText">'+ this.innerHTML+'</span><i class="layui-icon layui-icon-close my-close"></i></span>'
		$('.select .my-select-input').before(str)

		console.log(this.innerHTML)
		$('.my_option').hide();
	 });
	//  点击删除选中 点击删除选中事件
	$('.select').delegate('.my-close','click',function(e){ 
		e.stopPropagation()
		$('.select .xiaoSpan').remove();
	})
	//  实时监听输入框 监听下拉事件
	$(".my-select-input").bind("input propertychange",function(){
		if($(this).val() != ''){
		$('.my_option').hide();
		$('.my_option2').show();
		var str2 = $(this).val()
		$('.list-item2').text(str2)
		}else{
			$('.my_option2').hide();
			$('.my_option').show();
		}
	})
	$('.list-item2').click(function(e){ //输入标题的时候,非下拉 显示事件
		e.stopPropagation()
		$('.select .xiaoSpan').remove();
		var str = '<span class="xiaoSpan"><span class="spanText">'+ this.innerHTML+'</span><i class="layui-icon layui-icon-close my-close"></i></span>'
		$('.select .my-select-input').before(str)
		$('.my_option2').hide();
		$('.my-select-input').val('')
	})

		//回车键事件
		$('.my-select-input').keyup(function(){
			if(event.keyCode==13 && $(this).val() != ''){
				$('.select .xiaoSpan').remove();
				var str = '<span class="xiaoSpan"><span class="spanText">'+ $(this).val()+'</span><i class="layui-icon layui-icon-close my-close"></i></span>'
				$('.select .my-select-input').before(str)
				$('.my_option2').hide();
				$('.my-select-input').val('')
			}
		});
	// input 摁下事件
	// $(".my-select-input").keydown(function(){
	// 	$('.my_option').show();
	// 	$('.my_option dd').hide();
	// })
});  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值