div+css实现一个可输入的下拉框

原文地址:http://xiguabaobao.com/46.html

div+css实现一个可输入的下拉框

不使用select元素,因为其无法输入且难以样式化,改用input元素组合一个列表元素ul来实现可输入的下拉框。

<!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>div+css实现一个可输入的下拉框</title>
<style type="text/css">
/*Reset*/
* {
	margin:0;
	padding:0;
}
ul {
	list-style:none;
}
/*op*/
.op {
	width:230px;
	margin:10%;
	border:1px solid #ccc;
}
.op-result {
	width:200px;
	padding:12px 15px;
	border:none;
	outline:none;
}
.op-list {
	width:100%;
}
.hide {
	display:none !important;
}
.op-list-item {
	padding:12px 15px;
	background-color:#f3f3f3;
	cursor:pointer;
}
.op-list-item:hover {
	background-color:#e3e3e3;
}
</style>
<script type="text/javascript">
function op_init(op){
    if (!op) return;
    var op_list = op.children[1];
    if(op_list.className.indexOf('hide') > -1) {
        op_list.className = op_list.className.replace(/(^|\s)hide(\s|$)/,'');
    } else {
        op_list.className += ' hide';
    }

    op_list.onclick = function(event){
        if(event.target.className.indexOf('op-list-item') > -1){
            op.children[0].value = event.target.innerHTML;
        }
    }
}
</script>
</head>

<body>
<div class="op" onclick="op_init(this);">
    <input class="op-result" type="text" value="Click to select"/>
    <ul class="op-list hide">
        <li class="op-list-item">a</li>
        <li class="op-list-item">b</li>
        <li class="op-list-item">c</li>
    </ul>
</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值