解决IE浏览器下select下拉框默认样式问题

来源:互联网 发布:游戏测试软件 编辑:程序博客网 时间:2024/05/17 16:02

在项目中,某个页面由于监控控件需要在IE上运行,导致IE上select下拉框无法清除样式,影响美观。
这里写图片描述
以css解决,出现以下问题:
1. select的默认样式清除,appearance:none,background:transparent;无法清除下拉箭头;
2. 在外层用div定宽使用overflow:hidden;将白色下拉框隐藏, 无奈点击select后,option框会上移,并且选中后option背景会盖住select背景图片,导致背景图片无效;

解决办法,使用js模拟select效果
这里写图片描述

html<div class="box">    <div class="problem"></div>    <ul class="item-wrap"></ul></div><div class="showMsg"></div>
css*{    margin: 0;    padding: 0;}li{    list-style: none;}.box{    width: 266px;    height: 43px;    position: absolute;    top: 50px;    left: 100px;}.problem{    width: 266px;    height: 43px;    line-height: 43px;    background: url(images/selectbg_266.png) no-repeat;    color: #ffffff;    position: relative;    top: 0;    left: 0;    text-indent: 10px;}.item-wrap{    width: 266px;    background: #013768;    line-height: 30px;    position: absolute;    top: 42px;    left: 0;    display: none;}.item-wrap li{    color: #ffffff;    text-indent: 10px;}.item-wrap li:hover{    background: #269ABC;}.item-wrap li:nth-of-type(even){    background: #013796;}
json数据[{"deptId":10001,"deptName":"选项一"},{"deptId":10002,"deptName":"选项二"},{"deptId":10003,"deptName":"选项三"},{"deptId":10004,"deptName":"选项四"},{"deptId":10005,"deptName":"选项五"}]
jsvar oUl=$(".item-wrap");$.ajax({    url:'js/data.json',    type:'GET',     dataType:'json',     success:function(result){        for(var i=0;i<result.length;i++){            var oli = "<li value="+'"'+result[i].deptId+'"'+">"+result[i].deptName+"</li>";            oUl.append(oli);        }        var checkedId=$('.item-wrap li[value="10004"]').attr("value");//按照需求选择预设值        var checkName=$('.item-wrap li[value="10004"]').text();        if(checkedId!=""){//若满足条件,取预设值            $(".problem").text(checkName);            $(".showMsg").html(checkedId);//预设id(调用函数)        }else{//否则选中第一项            var name=$($(".item-wrap").children("li").get(0)).text();            $(".problem").text(name);        }    }});$(".problem").click(function(e){    $(".item-wrap").fadeToggle(100);    e.stopPropagation();});//下拉框功能实现$(".item-wrap").on("click","li",function(e){    var checkVal=$(this).text();    var deptId=$(this).attr("value");//获取id(调用函数)    $(".showMsg").html(deptId);    console.log(deptId);    $(".problem").text(checkVal);//div赋值    $(".item-wrap").fadeOut();    e.stopPropagation();});

这里写图片描述

还原项目使用场景:
1. 使用ajax获取数据
2. 有预设值
3. 动态取得编号id

0 0
原创粉丝点击