码迷,mamicode.com
首页 > Web开发 > 详细

jquery+css实现下拉列表

时间:2017-09-03 12:23:47      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:hint   display   play   fun   bottom   nbsp   ready   width   rip   

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>fruit</title>
<style type="text/css">

.hide {
  display: none;
}
div {
  float: left;
  width: 100%;
}
.selector-containter {
  margin-bottom: 10px;
}
.selector {
  width: 200px;
  background: #FFF;
  border: 1px solid #DDD;
}
.selector-hint {
  width: 178px;
  border: 1px solid #DDD;
}
.selector-expand {
  width: 8px;
  border: 1px solid #DDD;
}
.selector-collapse {
  width: 8px;
  border: 1px solid #DDD;
}
</style>
<script src="jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function() {
    
    //使用on方法,采用事件委派机制,selector-option-container中的内容为后续动态追加
    $(‘.selector‘).on(‘click‘, ‘.selector-expand‘, function() {
        $(this).parent().children(‘.selector-option-container‘).children().remove();
        $(this).parent().children(‘.selector-option-container‘).append(‘<div><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></div><div class="selector-option">apricot</div>‘);
        $(this).parent().children(‘.selector-option-container‘).append(‘<div><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></div><div class="selector-option">banana</div>‘);
        
        $(this).nextAll(‘.selector-option-container‘).removeClass(‘hide‘);
    });

    $(‘.selector‘).on(‘click‘, ‘.selector-collapse‘, function() {
        $(this).nextAll(‘.selector-option-container‘).addClass(‘hide‘);
    });
    
    $(‘.selector-t1‘).on(‘click‘, ‘.selector-option‘, function() {
        $(this).parent().parent().children(‘.selector-hint‘).text($(this).text());
        
        $(this).parent().addClass(‘hide‘);
    });
    
    $(‘.selector-t1‘).on(‘click‘, ‘.selector-checkbox‘, function() {
        $(this).parent().parent().parent().children(‘.selector-hint‘).text($(this).parent().next().text());
        
        //采用prop方法,对于值为布尔型的属性赋值
        $(this).prop(‘checked‘, false);
        
        $(this).parent().parent().addClass(‘hide‘);
    });
});
</script>
</head>
<body>
<div id="titan" class="selector-containter">
<div id="fruit">
  <div class="selector">
    <div class="selector-hint">select fruit</div>
    <div class="selector-expand">+</div>
    <div class="selector-collapse">-</div>
    <div class="selector-option-container">
    </div>
  </div>
</div>
</div>
<div id="athena" class="selector-t1 selector-containter">
<div id="fruit">
  <div class="selector">
    <div class="selector-hint">select fruit</div>
    <div class="selector-expand">+</div>
    <div class="selector-collapse">-</div>
    <div class="selector-option-container">
    </div>
  </div>
</div>
</div>
</body>
</html>

jquery+css实现下拉列表

标签:hint   display   play   fun   bottom   nbsp   ready   width   rip   

原文地址:http://www.cnblogs.com/apricot/p/7469124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!