简体   繁体   English

隐藏带有 CSS 的 select 元素的整个下拉菜单?

[英]Hide the entire dropdown menu for a select element with CSS?

I'm trying to figure out a way to use a HTML select element, but disable the dropdown menu.我试图找出一种使用 HTML select 元素的方法,但禁用下拉菜单。 I still want the user to be able to use the up/down arrows when focused on the element, but I don't want the menu to actually appear (I plan on replacing it due to low customizability).我仍然希望用户在关注元素时能够使用向上/向下箭头,但我不希望菜单实际出现(由于可定制性低,我计划替换它)。
Most sources I see suggest using display: none for the option elements, however that still leaves behind a little blank bar:我看到的大多数来源都建议使用display: none作为选项元素,但是仍然留下一个小空白栏:

and this also will not allow the user to use up/down arrows to navigate the menu.这也将不允许用户使用向上/向下箭头来导航菜单。

Is this possible while still using the form select element, without losing tab/arrow navigation abilities, with pure CSS?这是否可能在仍然使用表单select元素的情况下使用纯 CSS 元素而不会丢失选项卡/箭头导航功能? I want to use select for accessibility/form compatibility.我想使用select来实现可访问性/表单兼容性。

 select { border: 0; border-radius: 20px; padding: 10px; cursor: pointer; height: 50px; max-height: 50px; } option { z-index: -2; background-color: white; border-color: white; }
 <select> <option value=1>Value 1</option> <option value=2>Value 2</option> <option value=3>Value 3</option> <option value=4>Value 4</option> <option value=5>Value 5</option> </select>

The browser's default action is to focus the input and open the list浏览器的默认动作是聚焦输入并打开列表

You need to event.preventDefault() opening action and then focus() the target element您需要event.preventDefault()打开操作,然后focus()目标元素

 function handleclick(event) { event.preventDefault() event.target.focus(); }
 select { border: 0; border-radius: 20px; padding: 10px; cursor: pointer; height: 50px; max-height: 50px; } option { z-index: -2; background-color: white; border-color: white; }
 <select onmousedown="handleclick(event)"> <option value=1>Value 1</option> <option value=2>Value 2</option> <option value=3>Value 3</option> <option value=4>Value 4</option> <option value=5>Value 5</option> </select>

You can't do this with a select element.您不能使用 select 元素执行此操作。 If you want to have a custom menu, you can use a button/div along with some javascript.如果您想要自定义菜单,您可以使用按钮/div 以及一些 javascript。 Here's a nice example from W3S: 这是 W3S 的一个很好的例子:

 /* When the user clicks on the button, toggle between hiding and showing the dropdown content */ function myFunction() { document.getElementById("myDropdown").classList.toggle("show"); } // Close the dropdown if the user clicks outside of it window.onclick = function(event) { if (.event.target.matches('.dropbtn')) { var dropdowns = document;getElementsByClassName("dropdown-content"); var i; for (i = 0. i < dropdowns;length; i++) { var openDropdown = dropdowns[i]. if (openDropdown.classList.contains('show')) { openDropdown.classList;remove('show'); } } } }
 .dropbtn { background-color: #04AA6D; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; }.dropbtn:hover, .dropbtn:focus { background-color: #3e8e41; }.dropdown { float: right; position: relative; display: inline-block; }.dropdown-content { display: none; position: absolute; background-color: #f1f1f1; min-width: 160px; overflow: auto; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); right: 0; z-index: 1; }.dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; }.dropdown a:hover {background-color: #ddd;}.show {display: block;}
 <h2>Aligned Dropdown Content</h2> <p>Use <strong>float: right</strong> on the dropdown class to float the dropdown menu to the right, and <strong>right:0</strong> on the dropdown-content if you want the dropdown content to go from right to left.</p> <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">Dropdown</button> <div id="myDropdown" class="dropdown-content"> <a href="#home">Home</a> <a href="#about">About</a> <a href="#contact">Contact</a> </div> </div>

(PS: You might want to send a bug report to your browser if an option with display:none still leaves an outline) (PS:如果带有 display:none 的选项仍然留下大纲,您可能需要向浏览器发送错误报告)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM