模擬select的功能,解決IE高度不相容的問題
阿新 • • 發佈:2019-02-05
<!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" />
<title>ul模擬select</title>
<script type="text/javascript" src="http://www.codefans.net/ajaxjs/jquery-1.6.2.min.js"></script>
<style type="text/css">
<!--
.select_box {width:150px; position:relative; margin:10px;padding:0; font-size:12px;}
.submit_box {width:180px; position:relative; margin:10px;padding:0; font-size:12px; text-align:center;}
ul,li {list-style-type:none; padding:0; margin:0}
.select_box input {cursor:pointer; display:block; line-height:25px; width:100%; height:25px; overflow:hidden;border:1px solid #ccc; padding-right:20px; padding-left:10px; background:url(http://www.codefans.net/jscss/demoimg/201112/select_input_bg.gif) no-repeat 160px center;}
.select_box ul {width:180px; position:absolute; left:0; top:25px; border:1px solid #ccc; background:#fff; overflow: hidden;display:none; background:#ebebeb; z-index:99999;}
.select_box ul li {display:block;height:30px;overflow:hidden;line-height:30px;padding-left:5px;width:100%;cursor:pointer;}
.hover {background:#ccc;}
-->
</style>
<script type="text/javascript">
$(document).ready(function(){
$(".select_box input").click(function(){
var thisinput=$(this);
var thisul=$(this).parent().find("ul");
if(thisul.css("display")=="none"){
if(thisul.height()>200){thisul.css({height:"200"+"px","overflow-y":"scroll" })};
thisul.fadeIn("100");
thisul.hover(function(){},function(){thisul.fadeOut("100");})
thisul.find("li").click(function(){thisinput.val($(this).text());thisul.fadeOut("100");}).hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");});
}
else{
thisul.fadeOut("fast");
}
})
$("#submit").click(function(){
var endinfo="";
$(".select_box input:text").each(function(i){
endinfo=endinfo+(i+1)+":"+$(this).val()+";\n";
})
alert(endinfo);
})
});
</script>
</head>
<body>
<div class="select_box"><input id="myselect" type="text" value="請選擇..." readonly="readonly">
<ul class="select_ul">
<li>選項一</li>
<li>選項二</li>
<li>選項三</li>
<li>選項四</li>
<li>選項五</li>
</ul>
</div>
<div class="select_box"><input id="myselect" type="text" value="請選擇..." readonly="readonly">
<ul class="select_ul">
<li>選項一</li>
<li>選項二</li>
</ul>
</div>
<div class="select_box"><input id="myselect" type="text" value="請選擇..." readonly="readonly">
<ul class="select_ul">
<li>選項一</li>
<li>選項二</li>
<li>選項三</li>
<li>選項四</li>
<li>選項五</li>
<li>選項一</li>
<li>選項二</li>
<li>選項三</li>
<li>選項四</li>
<li>選項五選項五選項五選項五選項五</li>
</ul>
</div>
<div class="submit_box"><input type="button" id="submit" value="提交資料" /></div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ul模擬select</title>
<script type="text/javascript" src="http://www.codefans.net/ajaxjs/jquery-1.6.2.min.js"></script>
<style type="text/css">
<!--
.select_box {width:150px; position:relative; margin:10px;padding:0; font-size:12px;}
.submit_box {width:180px; position:relative; margin:10px;padding:0; font-size:12px; text-align:center;}
ul,li {list-style-type:none; padding:0; margin:0}
.select_box input {cursor:pointer; display:block; line-height:25px; width:100%; height:25px; overflow:hidden;border:1px solid #ccc; padding-right:20px; padding-left:10px; background:url(http://www.codefans.net/jscss/demoimg/201112/select_input_bg.gif) no-repeat 160px center;}
.select_box ul {width:180px; position:absolute; left:0; top:25px; border:1px solid #ccc; background:#fff; overflow: hidden;display:none; background:#ebebeb; z-index:99999;}
.select_box ul li {display:block;height:30px;overflow:hidden;line-height:30px;padding-left:5px;width:100%;cursor:pointer;}
.hover {background:#ccc;}
-->
</style>
<script type="text/javascript">
$(document).ready(function(){
$(".select_box input").click(function(){
var thisinput=$(this);
var thisul=$(this).parent().find("ul");
if(thisul.css("display")=="none"){
if(thisul.height()>200){thisul.css({height:"200"+"px","overflow-y":"scroll" })};
thisul.fadeIn("100");
thisul.hover(function(){},function(){thisul.fadeOut("100");})
thisul.find("li").click(function(){thisinput.val($(this).text());thisul.fadeOut("100");}).hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");});
}
else{
thisul.fadeOut("fast");
}
})
$("#submit").click(function(){
var endinfo="";
$(".select_box input:text").each(function(i){
endinfo=endinfo+(i+1)+":"+$(this).val()+";\n";
})
alert(endinfo);
})
});
</script>
</head>
<body>
<div class="select_box"><input id="myselect" type="text" value="請選擇..." readonly="readonly">
<ul class="select_ul">
<li>選項一</li>
<li>選項二</li>
<li>選項三</li>
<li>選項四</li>
<li>選項五</li>
</ul>
</div>
<div class="select_box"><input id="myselect" type="text" value="請選擇..." readonly="readonly">
<ul class="select_ul">
<li>選項一</li>
<li>選項二</li>
</ul>
</div>
<div class="select_box"><input id="myselect" type="text" value="請選擇..." readonly="readonly">
<ul class="select_ul">
<li>選項一</li>
<li>選項二</li>
<li>選項三</li>
<li>選項四</li>
<li>選項五</li>
<li>選項一</li>
<li>選項二</li>
<li>選項三</li>
<li>選項四</li>
<li>選項五選項五選項五選項五選項五</li>
</ul>
</div>
<div class="submit_box"><input type="button" id="submit" value="提交資料" /></div>
</body>
</html>