jquery+css實現下拉式清單

來源:互聯網
上載者:User

標籤: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實現下拉式清單

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.