Posted in Javascript onJanuary 05, 2015
html代码:
<div class="dropdownContainer"> <div class="dropdownDefault">1</div> <span class="downArrow arrow"></span> <ul class="dropdrown-menu"> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">6</a></li> <li><a href="#">8</a></li> </ul> </div>
css代码:
.dropdownContainer{position: relative;height: 30px;width: 100%;background: #555;} .dropdownDefault{border:1px solid #ddd;line-height: 28px;text-indent: 0.5em;} .dropdownContainer .downArrow{position: absolute;right: 5px;top: 9px} .dropdrown-menu{position: absolute;top:100%;width: 100%;left: 0;background: #555;display: none;} .dropdrown-menu li{line-height: 24px;} .dropdrown-menu li a{display: inline-block;width: 100%;text-indent: 0.5em} .dropdrown-menu li a:hover{background: #0078b6;font-size: 1.1em;} .arrow{width: 0;height: 0;display: inline-block;cursor: pointer;} .downArrow{border-left: 6px solid transparent;border-right: 6px solid transparent;border-top: 12px solid #fff;}
js(jquery)代码:
$(".dropdownDefault,.dropdownContainer .downArrow").click(function(){ $(this).siblings(".dropdrown-menu").show(); }); $(".dropdrown-menu li a").click(function(){ $(this).parent().parent().siblings(".dropdownDefault").html($(this).html()); $(this).parent().parent().hide(); });
虽然代码很简单,但是效果是不是非常酷呢
jQuery实现自定义下拉列表
- Author -
hebedich声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@