css实现下拉框,html中下拉框的代码
大家好,关于css实现下拉框很多朋友都还不太明白,今天小编就来为大家分享关于html中下拉框的代码的知识,希望对各位有所帮助!
怎样用cssselect下拉框的美化
用cssselect下拉框的美化
这个可以换种方式实现,首先select的样式每个浏览器都有其默认的样式,需要先去除这些默认样式,其次,select里面的样式诸如箭头,下拉框等等的样式,这里提供一种思路,就是在select的外层添加一个div,对这个div元素设置样式,select元素则是没样式,从而达到一种掩眼法的效果,实现方式如下:
<!-- html布局-->
<div id="selectStyle">
<select id="select">
<option>option 1</option>
<option>option 2</option>
<option>option 3</option>
<option>option 4</option>
<option>option 5</option>
</select>
</div>
首先要去掉#select的默认样式:
/*去掉默认样式,设置新的样式*/
#select{
display:block;
width:100%;
height:100%;
box-sizing:border-box;
background:none;
border:1px solid#222;
outline:none;
-webkit-appearance:none;
padding:0 5px;
line-height:inherit;
color:inherit;
cursor:default;
font-size:14px;
position:relative;
z-index:3;
}
#select option{
color:#222;
}
#select option:hover{
color:#fff;
}
#select option:checked{
background:#535353;
color:#fff;
}
然后在外层div#selectStyle设置自定义样式
#selectStyle{
display:block;
margin:0 auto;
overflow:hidden;
height:30px;
width:240px;
border-radius:0;
background:#535353 url("箭头图片地址") right center no-repeat;
background-size:auto 80%;
color:#fff;
line-height:2;
/*如果不想加图片,
则可以设置一个自己的三角形样式,
如下的自定义方式,
见代码1*/
position:relative;
z-index:1;
}
/*代码1*/
#selectStyle:before{
position:absolute;
z-index:1;
top:50%;
right:10px;
margin-top:-2.5px;
display:block;
width:0;
height:0;
border-style:solid;
border-width:5px 5px 0 5px;
border-color:#fff transparent transparent transparent;
content:"";
}
/*代码1*/
#selectStyle:after{
position:absolute;
z-index:1;
top:50%;
right:10px;
margin-top:-3.5px;
display:block;
width:0;
height:0;
border-style:solid;
border-width:5px 5px 0 5px;
border-color:#535353 transparent transparent transparent;
content:"";
}
以上就是自定义select样式的方法;
同时也可以完全不要select这个元素使用div+css来自定义一个跟select一样效果的下拉框(需要Javascript辅助)。
CSS技巧分享:如何用css制作横排二级下拉菜单
看到网站的导航按钮很炫酷不会怎么办?本片教程介绍如何使用css制作网站导航横排二级下拉菜单
打开Notepad++,先输入个页面框架!DOCTYPE html html xmlns="; hade/hade body/body
框架好了,那么就该定义页面的title,关键词keyword,和描述description meta charset="UTF-8" content="text/html" http-equiv="content-type" title纯css二级导航下拉菜单/title meta name="keyword" content="搜狗略懂" meta name="description" content="描述"这些内容只能在head/head中完成。
定义页面使用的css样式,也是需要在head里定义的。 style type="text/css"*{ margin:0; padding:0;}.jiao ul li a:hover{ color:#red;} ul, li{ list-style:none;} a{ text-decoration:none;}.jiao{ border:5px solid#pink; border-right:none; overflow:hidden; float:left; margin:200px 0 0 600px;}.jiao ul li ul li a{ border-right:solid; border-top:2px dotted#666; background:#black;}.jiaoulli{ float:left;}.jiao ul li a{ width:150px; height:80px; text-align:center; line-height:100px; display:block; border-right:5px solid#red; background:#red; color:#red;}.jiao ul li ul{ position:absolute; display:none;}.jiao ul li:hover ul{ display:block;}.jiaoulliulli{ float:none;}!--navul只选择nav下一级里面的ul元素 nav ul选择nav内所包含的所有ul元素 navul比nav ul限定更严格,必须后面的元素只比前面的低一个级别。这里根据你的需求来自己定义。--/style
添加一个DIV标签,在页面中划分出一个块来,用来显示。 div中所用的样式为刚才咱们声明的样式“jiao”这个盒子来描述
然后使用无序标签ul+样式li来实现模块。 div class="jiao" ul lia href="#"搜狗略懂/a ul lia href="#"搜狗问问/a/li lia href="#"搜狗输入法/a/li lia href="#"搜狗浏览器/a/li lia href="#"搜狐/a/li/ul/div
放上全部的代码吧,可以参考一下啊,!DOCTYPE html html xmlns="允许你通过一个网址来识别你的标记 head meta http-equiv="Content-Type" content="text/html; charset=utf-8"/ meta name="keyword" content="利用css实现下拉菜单" meta name="description" content="搜狗略懂、css分享" titlecss实现下拉导航栏菜单/title style type="text/css"*{ margin:0; padding:0;}.jiao ul li a:hover{ color:#red;} ul, li{ list-style:none;} a{ text-decoration:none;}.jiao{ border:5px solid#pink; border-right:none; overflow:hidden; float:left; margin:200px 0 0 600px;}.jiao ul li ul li a{ border-right:solid; border-top:2px dotted#666; background:#black;}.jiaoulli{ float:left;}.jiao ul li a{ width:150px; height:80px; text-align:center; line-height:100px; display:block; border-right:5px solid#red; background:#red; color:#red;}.jiao ul li ul{ position:absolute; display:none;}.jiao ul li:hover ul{ display:block;}.jiaoulliulli{ float:none;}!--navul只选择nav下一级里面的ul元素 nav ul选择nav内所包含的所有ul元素 navul比nav ul限定更严格,必须后面的元素只比前面的低一个级别。这里根据你的需求来自己定义。--/style/head body div class="jiao" ul lia href="#"搜狗略懂/a ul lia href="#"搜狗问问/a/li lia href="#"搜狗输入法/a/li lia href="#"搜狗浏览器/a/li lia href="#"搜狐/a/li/ul/li/ul/div/body/html
如果要添加更多的菜单,只需要后边继续添加ul/li即可
HTML 下拉 列表 框的问题
是可以实现的,只是比较麻烦,参考代码如下:
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
/* CSS Document*/
/* for input_dropMenu*/
body{
line-height:10px;
SCROLLBAR-FACE-COLOR:#eeeedd;
SCROLLBAR-HIGHLIGHT-COLOR:#eeeedd;
SCROLLBAR-SHADOW-COLOR:#666677;
SCROLLBAR-3DLIGHT-COLOR:#666677;
SCROLLBAR-ARROW-COLOR:#666677;
SCROLLBAR-TRACK-COLOR:#efefef;
SCROLLBAR-DARKSHADOW-COLOR:#eeeedd;
}
div{
font-size:9pt;
}
.dropMenu{
overflow:auto;
overflow-x:visible;
height:100%;
border-top:1px solid#aaaabb;
border-left:1px solid#aaaabb;
border-right:1px solid#666677;
border-bottom:2px solid#666677;
background-color:#fdfdfd;
FILTER:progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr=white,endColorStr=#eeeedd);
white-space:nowrap;
}
.dropMenu div{
cursor:Default;
color:#666677;
padding:1px 2px 1px 2px;
padding-top:2px;
script1:expression(
onmouseover=function(){
this.style.background="#0A246A"
this.style.fontWeight="normal"
this.style.color="white"
this.style.paddingLeft="3px"
},
onmouseout=function(){
this.style.background="none"
this.style.fontWeight="normal"
this.style.color="#666677"
this.style.paddingLeft="2px"
},
onclick=function(){
choose_dropMenu(this.parentNode.style.parent1,this)
}
);
}
</style>
</head>
<script language="JavaScript">
// JavaScript Document
/*
=== for input_dropMenu===
=== gotCode 2004-9-21===
*/
function drawShadowDiv(){
sDiv='<div id="shadowDiv"'+
'style="'+
'position:absolute;'+
'left:0px;top:0px;'+
'z-index:1;'+
'background-color:#2C6C85;'+
'layer-background-color:#2C6C85;'+
'display:none;'+
'FILTER:progid:DXImageTransform.Microsoft.Alpha(style=1,opacity=10,finishOpacity=20,startX=0,finishX=0,startY=0,finishY=100);'+
'border:1px solid skyblue;'+
'"></div>'
document.write(sDiv)
}
drawShadowDiv()
function giveItShaow( which, sDiv,type){
if( type=="show"){
sDiv.style.zIndex= which.style.zIndex
sDiv.style.left= which.offsetLeft+ which.parentNode.offsetLeft+ 2;
sDiv.style.top= which.offsetTop+ which.parentNode.offsetTop+ 3;
sDiv.style.width= which.offsetWidth;
sDiv.style.height= which.offsetHeight;
sDiv.style.display="";
}else{
sDiv.style.display="none"
sDiv.style.zIndex= 1
}
}
//===显示下拉框===
/*
current_dropMenu=页面当前下拉框的ID
close_dropMenu=是否可以关闭下拉框
dropMenuGo= setTimeout的变量
*/
var current_dropMenu= null
var close_dropMenu= true
var dropMenuGo= null
function show_dropMenu( which){
current_dropMenu= which
if( dropMenuGo!= null){
clearTimeout(dropMenuGo)
}
//info.innerText= which.style.script1
eval("outSide_"+which.id).style.display=""
which.style.display=""
which.scrollTop= 0
marginTop= parseInt(which.style.marginTop)
if( marginTop<-3){
which.style.marginTop= marginTop+ Math.round(( 0- marginTop)/3)
}else if( marginTop>=-3&& marginTop< 0){
which.style.marginTop= marginTop+ 1
}
if( marginTop>= 0){
clearTimeout(dropMenuGo)
giveItShaow(which,shadowDiv,'show')
return
}
dropMenuGo= setTimeout("show_dropMenu("+which.id+")",10)//
}
//===隐藏下拉框===
function hide_dropMenu(){
if( current_dropMenu!= null){
if( close_dropMenu){
clearTimeout(dropMenuGo)
current_dropMenu.style.display="none"
eval("outSide_"+current_dropMenu.id).style.display="none"
current_dropMenu.style.marginTop= current_dropMenu.parentNode.offsetHeight*(-1)
giveItShaow(current_dropMenu,shadowDiv,'hide')
}
}
}
//===选择下拉框的值===
function choose_dropMenu( which, self){
close_dropMenu= true
hide_dropMenu()
//eval(which).document.body.innerText= self.innerText.substr(0,self.innerText.length-1)
eval(which).value= self.innerText
}
//===除前后空格===
function trim(str){
if( str.substr(0,1)==""){
str= str.substr(1)
}
if( str.substr(str.length-1,1)==""){
str= str.substr(0,str.length-2)
}
return str
}
function replaceFilters( str){
//str= str.replace(//g,"")
str= trim(str)
str= str.replace(/=/g,':')
str= str.replace(/:/g,':"')
str= str.replace(//g,'')
str= str.replace(/:"\/\//g,'://')
str= str.replace(/javascript:"/g,'javascript:')
str= str.replace(/,/g,'",')
str= str+'"'
str= str.replace(/""/g,'"')
str="{"+ str+"}"
return str
}
/*
type=变量名称
defaultVariables=默认的值(不能为空)
*/
function setFilters( type, defaultVariables){
if( filters[type]== undefined){
if( defaultVariables== undefined){
defaultVariables=""
}
eval( type+"=defaultVariables")
}else{
eval( type+"=filters[type]")
}
}
var getStr, cTmpStr
function Separate_Str(){
getStr=""
for( i= 0; i< cTmpStr.length; i++){
if( cTmpStr.substr(i,1)==""){
cTmpStr= cTmpStr.substr(getStr.length+1)
return getStr
}
getStr= getStr+ cTmpStr.substr(i,1)
}return getStr
}
//===drawDropMenu===
/*
iAutoID=自动编号
id=输入框的值
*/
var iAutoID= 0;
function SS_drawDropMenu( filter){
iAutoID++
if( filter== undefined){
filters={}
}else{
eval("filters="+ replaceFilters( filter))
}
setFilters("id","inputFrame"+iAutoID)
setFilters("form","")
setFilters("maxlength","")
setFilters("cItem","没有选项")
setFilters("value","")
setFilters("startColor","white")
setFilters("endColor","#eeeedd")
setFilters("onfocus","'javascript:;'")
setFilters("onblur","'javascript:;'")
setFilters("onmouseover","'javascript:;'")
setFilters("onmouseout","'javascript:;'")
setFilters("onmouseup","'javascript:;'")
setFilters("onmousedown","'javascript:;'")
setFilters("onkeyup","'javascript:;'")
setFilters("onkeydown","'javascript:;'")
setFilters("onchange","'javascript:;'")
if( form!=""){
parent1= form+"."+id
}else{
parent1= id
}
//---建立下拉框选项的html语句---
var itemCount//-下拉框选项的循环次数
var itemHtml=""//-html语句
var dropMenuHeight=""
var inputSize= 0//-下拉框的高度
itemCount= cItem.split('').length
cTmpStr= cItem
for( var i= 0; i< itemCount; i++){
menuValue= Separate_Str()
itemHtml= itemHtml+'<div>'+menuValue+'</div>\n'
if( menuValue.length> inputSize){
inputSize= menuValue.length
}
}
if( itemCount< 5){
dropMenuHeight=(17* itemCount+3)
}else{
dropMenuHeight= 88
}
setFilters("size", inputSize*2)
//---end---
oHtml='<div id="input_dropMenu'+id+'" style="display:inline;height:21px;">\n'+
'<table border="0" cellspacing="0" cellpadding="0">\n'+
'<tr>\n'+
'<td style="font-size:9pt;border:1px solid black;border-right:0px;">\n'+
'<input name="'+id+'" type="text" value="'+value+'" size="'+size+'" maxlength="'+maxlength+'" style="font-size:9pt;border:0px solid black;"'+
' onfocus='+onfocus+
' onblur='+onblur+
' onmouseover='+onmouseover+
' onmouseout='+onmouseout+
' onmouseup='+onmouseup+
' onmousedown='+onmousedown+
' onkeyup='+onkeyup+
' onkeydown='+onkeydown+
' onchange='+onchange+
'>'+
'</td>\n'+
'<td width="16px" style="font-size:9pt;border:1px solid black;border-left:0px;border-right:0px;">\n'+
'<div style="'+
'font-family:webdings;'+
'overflow:hidden;'+
'height:18;'+
'margin-left:-1px;'+
'border:2px solid outset;'+
'FILTER:progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr='+startColor+',endColorStr='+endColor+');'+
'"'+
' onmousedown="this.style.border=\'2px inset\'"'+
' onmouseup="this.style.border=\'2px outset\'"'+
' onmouseover="this.style.border=\'2px outset\'"'+
' onmouseout="this.style.border=\'2px outset\'"'+
'>\n'+
'<div style="'+
'font-size:9pt;'+
'margin-top:-3px;'+
'margin-left:1px;'+
'cursor:hand;'+
'color:#444444'+
'"'+
' onClick="show_dropMenu(dropMenu'+id+')"'+
' onmousedown="this.style.marginLeft=\'2px\';"'+
' onmouseup="this.style.marginLeft=\'1px\'"'+
' onselectstart="return false"'+
'>6</div>\n'+
'</div>\n'+
'</td>\n'+
'</tr>\n'+
'<tr>\n'+
'<td colspan="2">\n'+
'<div id="outSide_dropMenu'+id+'"'+
' class="outSideDropMenu"'+
' onselectstart="return false"'+
'style="'+
'display:none;'+
'position:absolute;'+
'z-index:21;'+
'margin-top:-1px;'+
'height:'+dropMenuHeight+'px;'+
'width:100%;'+
'overflow:hidden;'+
'overflow-x:visible;'+
'border:0px solid black;'+
'">\n'+
'<div id="dropMenu'+id+'" class="dropMenu" style="margin-top:expression(this.parentNode.offsetHeight*(-1));parent1:'+parent1+'" onMouseover="close_dropMenu=false;" onMouseout="close_dropMenu=true;">\n'+
itemHtml+
'</div>\n'+
'</div>\n'+
'</td>\n'+
'</tr>\n'+
'</table>\n'+
'</div>\n'
document.write(oHtml)
}
</script>
<body onMousedown="hide_dropMenu();">
<form name="form1" method="post" action="">
姓名:
<input name="cName" type="text" id="cName" size="10">
<br>
年龄:
<input name="iAge" type="text" id="iAge" size="10">
<br>
职位:<script>SS_drawDropMenu('id=kkk,form=form1,cItem=科员科长所长局长')</script><script>SS_drawDropMenu('id=kkk2,form=form1,cItem=科员科长所长局长')</script>
<input name="dd" type="button" value="提交" onclick="alert(form1.kkk.value)">
</form>
<p></p></body>
</html>
好了,文章到此结束,希望可以帮助到大家。