87,910
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Select</title>
<style>
.sel{
width: 130px;
font-size: 12px;
cursor: pointer;
color: #555;
margin: 10px;
}
.sel .result{
position: relative;
padding: 5px;
height: 20px;
border: 1px solid #bbb;
border-radius: 5px;
}
.sel .text{
}
.sel .arrow{
display: inline-block;
position: absolute;
right: 8px;
top: 10px;
width: 0;
height: 0;
border-left: 4px solid transparent;
border-right: 4px solid transparent;
border-top: 8px solid #999;
}
.sel .option{
padding: 0;
margin: 0;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 6px 12px rgba(0,0,0,.175);
position: relative;
top: 4px;
list-style: none;
display: none;
max-height: 200px;
overflow: scroll;
}
.sel .option li{
padding: 2px 5px;
}
.sel .option li:hover{
background: #eee;
}
</style>
</head>
<body>
<div class="sel"></div>
<div class="sel"></div>
<div class="sel"></div>
<button>获取选中的值</button>
<div id="show"></div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
(function($){
$.fn.dropdown = function(option){
var _this = this;
this.each(function(){
var defaults = {
data:[],
value:null,
placeholder:'请选择'
}
option = $.extend(defaults,option);
var innerHtml = [],self = $(this);
innerHtml.push('<div class="dropdown">');
innerHtml.push(' <div class="result"><span data-value="" class="text">' + option.placeholder + '</span><span class="arrow"></span></div>');
innerHtml.push(' <ul class="option">');
$(option.data).each(function(index,item){
innerHtml.push('<li data-value="' + item.id + '">' + item.text + '</li>');
})
innerHtml.push(' </ul>');
innerHtml.push('</div>');
$(this).html(innerHtml.join(''));
if(option.value){
$(option.data).each(function(index,item){
if(item.id == option.value){
$('.text',self).text(item.text);
$('.text',self).data('value',option.value);
}
})
}
$('.result',this).click(function(){
$('.dropdown .option').hide();
$('.option',self).slideToggle();
})
$('li',this).click(function(){
$('.text',self).text($(this).text());
$('.text',self).data('value',$(this).data('value'));
$('.option',self).hide();
})
})
return {
val:function(){
if(_this.length > 1){
return $('.text',_this).map(function(index,item){return $(item).data('value')}).get();
}
return $('.text',_this).data('value');
}
}
}
$(document).click(function(e){
var target = e.target||e.srcElement;
var isDropdown = $(target).closest('.dropdown').length == 0;
isDropdown && $('.dropdown .option').hide();
})
})(jQuery)
</script>
<script>
var list = [{id:1,text:'选项一'},{id:2,text:'选项二'},{id:3,text:'选项三'}];
var option = {
data:list,
value:3,
placeholder:'请选择选项'
}
var a = $('.sel').dropdown(option);
$('button').click(function(){
$('#show').html(a.val().join(','))
})
</script>
</body>
</html>
<!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 src="https://cdn.bootcss.com/jquery/1.7.1/jquery.min.js"></script>
<style>
/*tag begin*/
/*select_box begin*/
.select_box {
width: 150px;
position: relative;
padding: 0;
font-size: 12px;
}
.select_box > input {
cursor: pointer;
line-height: 25px;
width: 100%;
height: 25px;
overflow: hidden;
border: 1px solid #ccc;
padding-right: 10px;
padding-left: 10px;
}
.select_box ul, li {
list-style-type: none;
padding: 0;
margin: 0;
}
.select_box ul {
position: absolute;
left: 0;
top: 29px;
border: 1px solid #ccc;
overflow: hidden;
display: none;
background: #ebebeb;
z-index: 99999;
}
.select_box .arrows {
position: absolute;
border: 6px solid transparent;
top: calc(50% - 4px);
right: 0px;
border-top-color: black;
}
.select_box ul li {
display: block;
height: 30px;
overflow: hidden;
line-height: 30px;
padding-left: 5px;
cursor: pointer;
}
.select_box li:hover {
background: #ccc;
}
/*select_box begin*/
/*tag end*/
</style>
<title>ul模拟select</title>
</head>
<body>
<div class="select_box">
<div class="arrows"></div>
<input type="text" value="请选择..." readonly="readonly" />
<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="select_box">
<div class="arrows"></div>
<input type="text" value="请选择..." readonly="readonly" />
<ul>
<li>选项一</li>
<li>选项二</li>
<li>选项三</li>
<li>选项四</li>
<li>选项五</li>
<li>选项一</li>
<li>选项二</li>
<li>选项三</li>
<li>选项四</li>
<li>选项五选项五选项五选项五选项五</li>
</ul>
</div>
<script>
(function ($) {
$.fn.select = function (options) {
return this.each(function(){
var container = $(this), input = container.find('>input'),ul=container.find('>ul');
var ulheight = options.height == null ? 200 : options.height;
var inputBoxWidth = parseFloat(input.css("padding-right")) + parseFloat(input.css("padding-left")) + parseFloat(input.css("width"))
ul.css("width", inputBoxWidth + "px")
$(document).click(function (event) {
if (ul.css("display") != "none") {
if (!input.is(event.target)) {
ul.fadeOut("fast");
}
}
})
input.click(function () {
if (ul.css("display") == "none") {
if (ul.height() > parseFloat(ulheight)) {
ul.css({
height: ulheight + "px"
, "overflow-y": "scroll"
})
};
ul.fadeIn("100");
ul.find("li").click(function () {
input.val($(this).text());
ul.fadeOut("100");
})
}
else {
ul.fadeOut("fast");
}
})
});
}
})(jQuery)
</script>
<script type="text/javascript">
$('.select_box').select({ height: "200" })
</script>
</body>
</html>