Jquery + css implementation drop-down list (updated), jquerycss

Source: Internet
Author: User

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>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.