限制select显示条数:div完全模拟select

在做项目时我们有时会碰上这样的问题,如何限制select的选择条数,如果光凭借JS,JQ恐怕无法解决该问题

现在我们提供一种解决方法:

            用div完全模拟select ( 即用<div>配上CSS和jQuery,来打造出一个仿真的<select> ),来达到控制条数的效果。

注:如果你想让其select样式变的更漂亮,也要用到 div完全模拟select。

样式如图:例如选择English

(1)(2)(3)

代码如下:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
<script type="text/javascript" src="./js/jquery-1.8.3.min.js"></script>
<style>
/*div完全模拟select*/
.select-ipt {
	color:#000;
	background: #FFF5E6;
	cursor:pointer;
	width:135px;
	height: 17px;
	margin-top:1.5%;
	margin-left: 31px;
	line-height:17px;
	font-size:12px;
	border: 1px solid #000;
        border-radius: 2px; 
}
.selt{
	width: 98%;
	padding-left: 3px;
}
/*CSS小三角*/
span.triangle-down {
    display: inline-block;
    width: 0;
    height: 0;
    float: right;
    position: relative;
    right:5px;
    top:6px;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid #666;
}
span.triangle-down:hover{
	border-top: 6px solid #002E59;
}
.select-list {
	border: 1px #ccc solid;
	width:132px;	
	max-height: 130px;
	overflow-y:auto;
	margin-left:0px;
	position:relative;
	left: 32px;
	z-index:10000;
	background:#fff;
	border-radius: 0 0 3px 3px;
}
.select-list span{
	cursor:pointer;
	display:block;
	width:98%;
	height:17px;
	line-height:17px;
	padding:0;
	font-size:12px;
	margin-top: 3px;
	padding-left:3px;
}
.select-list span:hover{
	background:#f6615b;
	color:#fff
}
</style>

<script type="text/javascript">
	$(document).ready(function(){
		/*div完全模拟select*/
		$(".select-ipt").toggle(function(){ 
        	$('.select-list').show(); 
    	},function(){ 
       		$('.select-list').hide();
    	});

	   $(".option").click(function() {
			$(".selt").html(this.innerHTML);
			$(".select-list").hide();
		});
	   $("div:not(.select-ipt)").click(function() {
			$(".select-list").hide();
		});
	});	
</script>

</head>
<body>
	<div id="select-box">
	    <div class="select-ipt">
	    	<span class="selt">Select Experiment</span>
	    	<span class="triangle-down"></span><!-- CSS小三角 -->
	    </div>
	    <div style="display: none " class="select-list"><!--下拉框-->
	    	<span class="option" id="0">Select Experiment</span>
            <span class="option" >Chinese</span>
            <span class="option" >English</span>
            <span class="option" >Match</span>
            <span class="option" >JAY</span>
            <span class="option" >VAE</span>
	    </div>
	</div>
</body>
</html>


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值