Home >Web Front-end >JS Tutorial >Jquery+HTML+CSS implements drop-down list function

Jquery+HTML+CSS implements drop-down list function

php中世界最好的语言
php中世界最好的语言Original
2018-04-19 15:37:001723browse

This time I will bring you Jquery HTML CSS to implement the drop-down list function. What are the precautions for Jquery HTML CSS to implement the drop-down list function? The following is a practical case. Let’s take a look.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>fruit</title>
<style type="text/css">
.hide {
 display: none;
}
p {
 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('<p><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></p><p class="selector-option">apricot</p>');
    $(this).parent().children('.selector-option-container').append('<p><input type="checkbox" name="fruitGroup" class="selector-checkbox"/></p><p class="selector-option">banana</p>');
    $(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>
<p id="titan" class="selector-containter">
<p id="fruit">
 <p class="selector">
  <p class="selector-hint">select fruit</p>
  <p class="selector-expand">+</p>
  <p class="selector-collapse">-</p>
  <p class="selector-option-container">
  </p>
 </p>
</p>
</p>
<p id="athena" class="selector-t1 selector-containter">
<p id="fruit">
 <p class="selector">
  <p class="selector-hint">select fruit</p>
  <p class="selector-expand">+</p>
  <p class="selector-collapse">-</p>
  <p class="selector-option-container">
  </p>
 </p>
</p>
</p>
</body>
</html>

                                                                                                                                        

Recommended reading:

jQuery to create page mask layer effect

##How to use keyboard events in jquery


jQuery determines whether to browse to the bottom of the web page

The above is the detailed content of Jquery+HTML+CSS implements drop-down list function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn