下拉框实现

本文介绍了一种使用HTML和CSS结合JavaScript实现自定义下拉框的方法,解决了原生下拉框样式不可定制的问题。通过ul和li元素模拟下拉菜单,并利用jQuery控制显示与隐藏及交互效果。

在工作中原生的下拉框完成不了  设计图的要求,select中option不能修改高度,因此需要自己模拟一个下拉框,用ul,li来实现一个下拉框的功能代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charse="utf-8">

<titile></title>

<style type="text/css">

*{padding:0;margin:0}

body{font-family:"微软雅黑";font-size:16px}

.box{width:600px;margin:200px auto;position:relative;}

.box input{font-weight:normal;font-size:16px;height:38px;border:1px solid #ccc;cursor:pointer;width:260px;box-shadow:0 0 3px #aaa;text-indent:15px;position:relative;font-family:"微软雅黑;"}

ul{border:1px solid #ccc;border-top:0;box-shadow:0 0 3px #aaa;lin-height:38px;width:260px;position:absolute;top:38px;background:#fff;list-style:none;text-indent:15px;display:none;}

ul li{cursor:pointer;}

li:hover{background:#aaa;}

.arrow{width:0px;height:0px;boder-width:10px;border-style:solid dashed dashed dashed;border-left-corlor:transparent;border-right-color:transparent;border-botttom-color:transparent;}

.arrowTop{border-bottom-color:black;border-top-color:transparent;top:2px}

</style>

<head>

<body>

<div class="box">

<input value="english"  class="inputValue" readonly="readonly" type="text"/>

<ul id="selectLanByLoginUl">

<li data-value="1"></li>

<li data-value="2"></li>

</ul>

</div>

<div class="arrow"></div>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript" src="test.js"></script>




=============================test.js中代码如下==================


$(function(){

//点击弹出下拉框:

$(".inputValue").unbind("click").click(function(){

if($(".box").find("ul").css("display")=="none"){

$(".box").find("ul").show();

//增加箭头样式

$(".arrow").addClass("arrowTop");

}else{

$(".box").find("ul").hide();

//去掉向上的箭头改成向下的

$(".arrow").removeClass("arrowTop");

}

})

$(".box").bind({

//"mouseenter":function(){

//$(this).find("ul").show();

//},

//"mouseleave":function(){

//$(this).find("ul").hide();

//},

"click":function(event){

if(event.target.tagName=="LI"){

$(this).find("input").val($(event.target).text());

$(this).find("ul");

}

//$(this).find("ul");

}

})

$("#selectLanByLoginUl li").each(function(){

if($(this).text()=="spanish"){

$(this).find("input").val($(this).text());

}

})

})


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值