欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jquery+css实现下拉列表功能

程序员文章站 2022-09-08 10:27:38
废话不多说了,直接给大家贴代码了,具体代码如下所述: <...

废话不多说了,直接给大家贴代码了,具体代码如下所述:

<!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实现下拉列表功能,希望对大家有所帮助