Jquery + css implementation drop-down list (updated), jquerycss
I. Overview
Compared with the select drop-down list, the drop-down list implemented by jquery + css has better flexibility. The second part of the code is the implementation of the drop-down list.
Ii. Code
The drop-down list is as follows:
The options in the drop-down list are dynamic append. The on method is used, and the event delegation mechanism is used to respond to the Click Event of the option.
After testing, the following code can run normally in Firefox 55.0 and Safari 10.1.1.
Note: compared with the previous version, this update adds a scroll bar to the drop-down list.
Note: Make sure jquery works properly.
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> selector </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 div {
Height: 20px;
Line-height: 20px;
Padding: 10px 0 10px 0;
}
. Selector-hint {
Width: 158px;
Border: 1px solid # DDD;
Border-right: 0px;
}
. Selector-expand {
Width: 38px;
Border: 1px solid # DDD;
Text-align: center;
}
. Selector-collapse {
Width: 38px;
Border: 1px solid # DDD;
Text-align: center;
}
. Selector-option-container {
Width: 200px;
Height: 80px! Important;
Padding: 0px! Important;
Overflow-y: scroll;
}
. Selector-option-container div {
Border-bottom: 1px solid # E8E8E8;
}
. Selector-option {
Width: 160px;
Height: auto;
Min-height: 20px;
}
. Selector-checkbox-container {
Width: 18px;
}
. Selector-checkbox {
Height: 12px;
Padding: 0;
Margin: 0;
}
</Style>
<Script src = "jquery-3.2.1.min.js"> </script>
<Script>
$ (Document). ready (function (){
$ ('. Selector'). append ('<div class = "selector-hint"> select fruit </div> ');
$ ('. Selector'). append ('<div class = "selector-expand"> + </div> ');
$ ('. Selector'). append ('<div class = "selector-collapse hide">-</div> ');
$ ('. Selector'). append ('<div class = "selector-option-container hide"> </div> ');
// Use the on method and the event Delegate mechanism. The content in selector-option-container is dynamically appended.
$ ('. Selector'). on ('click', '. selector-expand', function (){
$ (This). nextAll ('. selector-option-iner'). children (). remove ();
$ ('. Selector-option-iner'). children (). remove ();
$. Each (['mongocot ', 'Banana', 'pear ', 'melo'], function (index, value ){
$ ('. Selector-option-iner '). append ('<div class = "selector-checkbox-container"> <input type = "checkbox" name = "fruitGroup" class = "selector-checkbox"/> </div> <div class = "selector-option"> '+ value +' </div> ');
});
$ (This). addClass ('hide ');
$ (This). next (). removeClass ('hide ');
$ (This). nextAll ('. selector-option-iner'). removeClass ('hide ');
});
$ ('. Selector'). on ('click', '. selector-collapse', function (){
$ (This). addClass ('hide ');
$ (This). prev (). removeClass ('hide ');
$ (This). nextAll ('. selector-option-iner'). addClass ('hide ');
});
$ ('. Selector-t1'). on ('click', '. selector-option', function (){
$ (This). parent (). parent (). children ('. selector-hint'). text ($ (this). text ());
$ (This). parent (). addClass ('hide ');
$ (This). parent (). prev (). addClass ('hide ');
$ (This). parent (). prev (). prev (). removeClass ('hide ');
});
$ ('. Selector-t1 '). on ('click','. selector-checkbox', function (){
$ (This ). parent (). parent (). parent (). children ('. selector-hint '). text ($ (this ). parent (). next (). text ());
// Use the prop method to assign values to properties with Boolean values
$ (This). prop ('checked', false );
$ (This). parent (). parent (). addClass ('hide ');
$ (This). parent (). parent (). prev (). addClass ('hide ');
$ (This). parent (). parent (). prev (). prev (). removeClass ('hide ');
});
});
</Script>
</Head>
<Body>
<Div id = "titan" class = "selector-containter">
<Div>
<Div class = "selector"> </div>
</Div>
</Div>
<Div id = "athena" class = "selector-t1 selector-containter">
<Div>
<Div class = "selector"> </div>
</Div>
</Div>
</Body>
</Html>