Home > Article > Web Front-end > Tutorial on implementing drop-down list function with jquery and css
Drop-down lists can be implemented in many languages. This article introduces the drop-down list function implemented by combining jquery and css through example code. It is very good and has reference value. Friends who need it can refer to it. I hope it can help everyone. .
Without further ado, I will post the code directly for you. The specific code is as follows:
<!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>
Related recommendations:
How to implement the WeChat applet display drop-down list function
Native js implementation of the drop-down list box
javascript for HTML drop-down List tag operations
The above is the detailed content of Tutorial on implementing drop-down list function with jquery and css. For more information, please follow other related articles on the PHP Chinese website!