
(HTML原生select标签)

select原始功能是可以点击右侧的下拉箭头可以打开下拉列表,给用户选择。用户选择一个选项,文本框里面的值要变成对应的option的内容,并且可以获取到option的value值。另外一个很重要的功能是option有一个selected属性。如果某个option有selected=”selected”,那么下拉框默认会选中当前这个option。
Bootstrap提供Button Dropdown是为了来做漂亮的菜单,压根不是想做select的功能。当我搜索看到它时,我还以为Bootstrap可以提供类似button dropdown样式的select组件。不过我是妄想,没有这个组件,必须组件实现。其实有过用ul和li写select标签的经验,写这个也不是很困难。
<div class="dropdown" style="margin-top:30px;"><a style="font-size:14px;" class="btn dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">Text 1<span class="caret"></span></a><ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"><li role="presentation" value=><a role="menuitem" tabindex="1" href="#">Text 1</a></li><li role="presentation"><a role="menuitem" tabindex="2" href="#">Text 2</a></li><li role="presentation"><a role="menuitem" tabindex="3" href="#">Text 3</a></li></ul></div>这个是Button Dropdown的基本代码。因为它本身会应用css样式,而已我们后面要改bootstrap.min.css里面的样式,所以我们需要在这个页面单独放一些样式。
<!DOCTYPE html><html><head><title>Bootstrap 101 Template</title><meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8"><!-- Bootstrap --><link href="css/bootstrap.css" rel="stylesheet"><style type="text/css">.btn-group.open .dropdown-toggle{-webkit-box-shadow:none;box-shadow:none;}</style><!--[if lt IE 9]><script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script><script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script><![endif]--></head><body><p>原始的select标签</p><select><option value="1">Text 1</option><option value="2">Text 2</option><option value="3">Text 3</option></select><div id="dropdown1" class="dropdown" style="margin-top:30px;"><div class="btn-group"><a style="font-size:14px;" class="btn dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown"><!--选中option之后,要在这里显示选中值,类似原始select里面的文本框--> <span class="placeholder">Text 1</span><span class="caret"></span></a><ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"><li role="presentation" value="1"><a role="menuitem" tabindex="1" href="javascript:void(0);">Text 1</a></li><li role="presentation" value="2"><a role="menuitem" tabindex="2" href="javascript:void(0);">Text 2</a></li><li role="presentation" value="3"><a role="menuitem" tabindex="3" href="javascript:void(0);">Text 3</a></li></ul></div></div><!-- jQuery (necessary for Bootstrap"s JavaScript plugins) --><script src="js/jquery-1.10.2.js"></script><!-- Include all compiled plugins (below), or include individual files as needed --><script src="js/bootstrap.min.js"></script><script type="text/javascript">function customDropDown(ele){this.dropdown=ele;this.placeholder=this.dropdown.find(".placeholder");this.options=this.dropdown.find("ul.dropdown-menu > li");this.val="";this.index=-1;//默认为-1;this.initEvents();}customDropDown.prototype={initEvents:function(){var obj=this;//这个方法可以不写,因为点击事件被Bootstrap本身就捕获了,显示下面下拉列表obj.dropdown.on("click",function(event){$(this).toggleClass("active");});//点击下拉列表的选项obj.options.on("click",function(){var opt=$(this);obj.text=opt.find("a").text();obj.val=opt.attr("value");obj.index=opt.index();obj.placeholder.text(obj.text);});},getText:function(){return this.text;},getValue:function(){return this.val;},getIndex:function(){return this.index;}}$(document).ready(function(){var mydropdown=new customDropDown($("#dropdown1"));});</script></body></html>点击过程中会出现一个背景。通过Chrome查看元素,是写box-shodow的效果。但是我改过之后,还是出现了。明天继续查找一下。
添加1行样式覆盖,bootstrap.css里面本身的样式。
<style type="text/css">.btn-group.open .dropdown-toggle{-webkit-box-shadow:none;box-shadow:none;}</style>注意事项:在实例化的过程中,我们传入的是一个jQuery选择器的对象,所以如果一个页面有很多自定义的dropdown,会使用类别。那在实例化的过程要修改一下代码哦!
Bootstrap官网 http://getbootstrap.com/components/#btn-dropdowns