读书人

使用CSS跟jQuery 模拟select

发布时间: 2013-10-19 20:58:23 作者: rapoo

使用CSS和jQuery 模拟select

模拟select 并带有提交后取得数据的代码

使用CSS跟jQuery 模拟selectXML/HTML Code
  1. <div id="dropdown">
  2. <p>请选择城市</p>
  3. <ul>
  4. <li><a href="#" rel="2">北京</a></li>
  5. <li><a href="#" rel="3">上海</a></li>
  6. <li><a href="#" rel="4">武汉</a></li>
  7. <li><a href="#" rel="5">广州</a></li>
  8. </ul>
  9. </div>
  10. <div id="result"></div>

JavaScript Code
  1. <script type="text/javascript">
  2. $(function(){
  3. $("#dropdown p").click(function(){
  4. var ul = $("#dropdown ul");
  5. if(ul.css("display")=="none"){
  6. ul.slideDown("fast");
  7. }else{
  8. ul.slideUp("fast");
  9. }
  10. });
  11. $("#dropdown ul li a").click(function(){
  12. var txt = $(this).text();
  13. $("#dropdown p").html(txt);
  14. var value = $(this).attr("rel");
  15. $("#dropdown ul").hide();
  16. $("#result").html("您选择了"+txt+",值为:"+value);
  17. });
  18. });
  19. </script>

CSS Code
  1. #dropdown{width:186px; margin:100px auto; position:relative}
  2. #dropdown p{width:150px; height:24px; line-height:24px; padding-left:4px; padding-right:30px; border:1px solid #a9c9e2; background:#e8f5fe url(arrow.gif) no-repeat rightright 4px; color:#807a62; cursor:pointer}
  3. #dropdown ul{width:184px; background:#e8f5fe; margin-top:2px; border:1px solid #a9c9e2; position:absolute; display:none}
  4. #dropdown ul li{height:24px; line-height:24px; text-indent:10px}
  5. #dropdown ul li a{display:block; height:24px; color:#807a62; text-decoration:none}
  6. #dropdown ul li a:hover{background:#c6dbfc; color:#369}
  7. #result{margin-top:10px;text-align:center}


原文地址:http://www.freejs.net/article_biaodan_60.html

读书人网 >CSS

热点推荐