div模拟下拉框的Demo

前两天在项目中碰到一个问题,在原图中下拉框的高度蛮高的,而用select下拉框,它的高度不够高,于是想控制它的样式,用了height,谁知道在IE6 7 下不可行。网上找了一下,方案有两种,一种是改变下拉框里面字体的大小,当字变大时,下拉框亦会变高,即控制下font-size就好了,第二种是用div+css+js来模拟下拉框,当然它的缺陷是没有像下拉框那样有value值,传值的时候不是太方便。我采用了第二种方案,代码如下:

<!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=gb2312" />
<script language="javascript" type="text/javascript">
function show_select(input,btn,option,value){
inputobj=document.getElementById(input);
btnobj=document.getElementById(btn);
optionobj=document.getElementById(option);
valueobj=document.getElementById(value);
optionobj.style.display=optionobj.style.display==""?"none":"";
optionobj.onblur=function () {
optionobj.style.display="none";
}

for (var i=0;i<optionobj.childNodes.length;i++){
optionobj.focus();
optionobj.childNodes[i].onmouseover=function (){
this.className="qty_items_over";
}
optionobj.childNodes[i].onmouseout=function (){
this.className="qty_items_out";
}
optionobj.childNodes[i].onclick=function () {
//alert(this.innerHTML)
inputobj.innerHTML=this.innerHTML;
valueobj.value=this.innerHTML;
optionobj.blur();
optionobj.style.display="none";
if (input=="pro_color"){
loadpic(this.id,"");
}
}
}
}
</script>
<style type="text/css">
body{
font-size:12px;
font:12px Arial, Helvetica, sans-serif;
}
.pro_select{
width:91px;
height:24px;
}
#pro_qty{
float:left;
height:22px;
width:65px;
text-align:center;
border-top:1px solid #B7D1EA;
border-bottom:1px solid #B7D1EA;
border-left:1px solid #B7D1EA;
line-height:22px;

}
#pro_qty_but{
float:left;
width:25px;
cursor:pointer;}
/*子项*/
#qty_items{
clear:left;
width:86px;
border-right:1px solid #B7D1EA;
border-bottom:1px solid #B7D1EA;
border-left:1px solid #B7D1EA;
position:absolute;
z-index:80;
}
.qty_items_out {
background-color:#FFFFFF;
margin-left:20px;
cursor:pointer;
line-height:22px;
}
.qty_items_over{
background-color:#CCCCCC;
cursor:pointer;
line-height:22px;
text-indent:20px;
}
.clear{
clear:left;}
</style>
</head>
<body>
<div class="pro_select">
<div id="pro_qty">企 业</div>
<div id="pro_qty_but" onclick="show_select('pro_qty','pro_qty_but','qty_items','qty')">
<img src="img/xiala.jpg" width="22" height="24" /></div>
<div class="clear"></div>
<div id="qty_items" style="display:none">
<div class='qty_items_out'>企 业</div>
<div class='qty_items_out'>产 品</div>
</div>
</div>
<form id="form1" name="form1" method="post" action="">
<input name="qty" type="hidden" id="qty" value="1" />
</form>
</body>
</html>


这个代码在理解上不成什么问题的,大家可以在这个的基础上进行修改,来达到自己想要的效果!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值