The current better way to make select list all options when selected/focused

The current better way to make select list all options when selected/focused
During development, I encountered such a requirement, so I recorded it for future use.

Demand background

Use the keyboard shortcut to navigate to the payment method selection box (a drop-down list) on the page and select it.

Technical Difficulties

Currently, browsers do not support listing all options under a drop-down list by locating it through code, and it can only be done by mouse click.

After searching for some information online, I came up with the best way to deal with it.

Use the size attribute of select and the position attribute of box layout to achieve this. The specific code is as follows:

Copy code
The code is as follows:

<td align="right">
Payment Methods:
</td>
<td style="padding:0px;vertical-align:top;">
<!-- You must wrap the select with a div, otherwise it will not be compatible with FF-->
<span style="color:#ff0000"><div style="position:relative;padding:1px;">
</span> <select id="payType" name="payType" style="position:absolute;" onfocus="expand(this)" onblur="unexpand(this)">
<option>RMB</option>
<option>US dollars</option>
<option>Credit card</option>
<option>Hong Kong Dollar</option>
<option>Hong Kong Dollar</option>
</select>
<span style="color:#ff0000"></div>
</span></td>

The expand and unexpand methods are both simple:

Copy code
The code is as follows:

function expand(obj){
$(obj).attr("size","10");
}
function unexpand(obj){
$(obj).attr("size","1");
}

Set the select position to absolute so that it does not affect the DOM flow layout. Then set the position of its container to relative so that the select is positioned according to its container.

It should be noted here that div must be used as the container of the select in the table element, because according to the w3c CSS standard, setting position:relative on table-related elements is undefined, so under ff the select element will be directly positioned according to the body element.

References:

http://www.php-insite.com/autoexpand_select.html View the page source code directly
http://bbs.csdn.net/topics/330158935 Pay attention to lingshuo1993's answer

<<:  Detailed explanation of VUE Token's invalidation process

>>:  Using CSS3 to achieve transition and animation effects

Recommend

JavaScript+HTML to implement student information management system

Table of contents 1. Introduction 2. Rendering 3....

Detailed explanation of MySQL combined index and leftmost matching principle

Preface I have seen many articles about the leftm...

Example statements for indexes and constraints in MySQL

Foreign Keys Query which tables the primary key o...

web.config (IIS) and .htaccess (Apache) configuration

xml <?xml version="1.0" encoding=&qu...

Web front-end performance optimization

Web front-end optimization best practices: conten...

Detailed explanation of MySQL batch SQL insert performance optimization

For some systems with large amounts of data, the ...

5 MySQL GUI tools recommended to help you with database management

There are many database management tools for MySQ...

How to implement nginx smooth restart

1. Background During the server development proce...

Detailed graphic explanation of sqlmap injection

Table of contents 1. We found that this website m...

CSS eight eye-catching HOVER effect sample code

1. Send effect HTML <div id="send-btn&quo...

MySql 8.0.16-win64 Installation Tutorial

1. Unzip the downloaded file as shown below . 2. ...

How to load the camera in HTML

Effect diagram: Overall effect: Video loading: Ph...

How to introduce scss into react project

First download the dependencies yarn add sass-loa...

Solution to the problem that mysql local login cannot use port number to log in

Recently, when I was using Linux to log in locall...